PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > const as array size
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
const as array size

Réponse
 
LinkBack Outils de la discussion
Vieux 23/02/2008, 15h21   #1
asit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut const as array size

We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}
}

Why the above code shows an error ??

How can we overcome it ??
  Réponse avec citation
Vieux 23/02/2008, 15h39   #2
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

asit wrote:
> We know that array size can be a constant integer.


No. The array size must be resolvable at compile time. There's a
difference.

> Consider the following case ??
>
> calss myc
> {
> const int size;
> int arr[size];
> public:
> myc(int x):size(x){}
> }
>
> Why the above code shows an error ??


Because the value of 'size' cannot be resolved at compile time. The
compiler cannot know how big 'arr', and consequently, how big 'myc' is.
It cannot instantiate it because it can't know how big it should be.

> How can we overcome it ??


If 'arr' has always the same size, let's say for example 123, do it
like this:

class myc
{
static const int size = 123;
int arr[size];
...
};

If what you want is a variable-sized array, you can't use that. You'll
have to use:

class myc
{
std::vector<int> arr;

public:
myc(int x): arr(x) {}
}
  Réponse avec citation
Vieux 23/02/2008, 15h40   #3
Kyle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

asit napisał(a):
> We know that array size can be a constant integer.
>
> Consider the following case ??
>
> calss myc
> {
> const int size;
> int arr[size];
> public:
> myc(int x):size(x){}
> }
>
> Why the above code shows an error ??


because array size needs to be a compile time constant integer

>
> How can we overcome it ??


by using std::vector<int>
  Réponse avec citation
Vieux 23/02/2008, 17h57   #4
Íõ¾ý³¼
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

On 2ÔÂ23ÈÕ, ÏÂÎç11ʱ21·Ö, asit <lipu...@gmail.com> wrote:
> We know that array size can be a constant integer.
>
> Consider the following case ??
>
> calss myc
> {
> const int size;
> int arr[size];
> public:
> myc(int x):size(x){}
>
> }
>
> Why the above code shows an error ??
>
> How can we overcome it ??

template<int SIZE>
class myc
{
int arr[SIZE];
//......
}
void main()
{
myc<6> c1;// arr size wil be 6;
myc<8> c2;// arr size will be 8;
}

  Réponse avec citation
Vieux 23/02/2008, 19h07   #5
acehreli@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

On Feb 23, 9:57 am, "Íõ¾ý³¼" <WangJunc...@gmail.com> wrote:
> On 2ÔÂ23ÈÕ, ÏÂÎç11ʱ21·Ö, asit <lipu...@gmail.com>wrote:
>
> > We know that array size can be a constant integer.

>
> > Consider the following case ??

>
> > calss myc
> > {
> > const int size;
> > int arr[size];
> > public:
> > myc(int x):size(x){}

>
> > }

>
> > Why the above code shows an error ??

>
> > How can we overcome it ??

>
> template<int SIZE>
> class myc
> {
> int arr[SIZE];
> //......}
>
> void main()


That's not C++! You might have meant to write

int main()

> {
> myc<6> c1;// arr size wil be 6;
> myc<8> c2;// arr size will be 8;


Not necessarily 6 or 8, unless it's a legal C++ program.

Ali
  Réponse avec citation
Vieux 24/02/2008, 07h12   #6
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

asit wrote:
> We know that array size can be a constant integer.


Size specifier in the array declaration must be an Integral Constant Expression.

> Consider the following case ??
>
> calss myc
> {
> const int size;
> int arr[size];
> public:
> myc(int x):size(x){}
> }
>
> Why the above code shows an error ??


Because in your case the size is not an Integral Constant Expression.

> How can we overcome it ??


There are many different ways. For example

#include <vector>

class myc
{
std::vector<int> arr;
public:
myc(int x) : arr(x) {}
};

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
Vieux 24/02/2008, 09h12   #7
Michael.Boehnisch@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

On 23 Feb., 16:21, asit <lipu...@gmail.com> wrote:
> We know that array size can be a constant integer.
>
> Consider the following case ??
>
> calss myc
> {
> const int size;
> int arr[size];
> public:
> myc(int x):size(x){}
>
> }
>
> Why the above code shows an error ??
>
> How can we overcome it ??


You cannot assign to size in the constructor by size(x) because it is
const - You cannot eat the fish and have it on the table at the same
time.
I recommend to just use an STL container for your problem, e.g.
vector<T>:

class myc {

std::vector<int> arr;

public:

myc( const int x ) : arr( x ) {}

}

best,

mu.
  Réponse avec citation
Vieux 24/02/2008, 18h59   #8
Thomas J. Gritzan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: const as array size

Michael.Boehnisch@gmail.com schrieb:
> On 23 Feb., 16:21, asit <lipu...@gmail.com> wrote:
>> We know that array size can be a constant integer.
>>
>> Consider the following case ??
>>
>> calss myc
>> {
>> const int size;
>> int arr[size];
>> public:
>> myc(int x):size(x){}
>>
>> }
>>
>> Why the above code shows an error ??
>>
>> How can we overcome it ??

>
> You cannot assign to size in the constructor by size(x) because it is
> const


Thats not an assignment, its initialization, and is the only way to set a
value to a const member in a class.
The error is in the array size specifier that must be a compile time constant.

> You cannot eat the fish and have it on the table at the same
> time.


I do that regularly.

> I recommend to just use an STL container for your problem, e.g.
> vector<T>:
>
> class myc {
>
> std::vector<int> arr;
>
> public:
>
> myc( const int x ) : arr( x ) {}
>
> }


Good suggestion, however.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
post tenebras lux. post fenestras tux.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13573 seconds with 16 queries