creating static array

R

Rahul Joshi

Hi,

I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?

Thanks,
Rahul
 
A

Andrey Tarasevich

Rahul said:
...
I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?
...

Aside from missing 'const' specifier and array size being zero, I don't
see any problems with this code. (Please, always post _real_ code, i.e.
the code you are actually trying to compile.)

The error message is most likely caused by a bug in your compiler.
According to 11/5 access control is not supposed to restrict access to
'A::size' in the above member definition.
 
X

xiaoqing Visson

i think you just make a mistake when you declare
static int array[size] ;
because std c++ doesn't support "dynamic" declare , you know size is not a
const number, so this will generate a mistake. and i compile it in dev-c++
4.9.8.0 with winme,
it just appear "4 D:\source code\stl_c++\statictest.h
size of member `array' is not constant"
so if you want to achive this ,i think the best way is to use stl: vector.


Rahul Joshi said:
Hi,

I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};
Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?

Thanks,
Rahul
 
R

Rahul Joshi

Rahul said:
...
I want to create a static array whose size is also a const
member of the class. Something like:

// A.h
class A {
private:
static int size = 0;
static int array[size];
};

Then I need to define the static array in A.cpp like:

int A::array[A::size];

However, the compiler complains that A::size is a private
and cannot be accessed. So how can I define such a static
array without making the size of the array public ?
...

Aside from missing 'const' specifier and array size being zero, I don't
see any problems with this code. (Please, always post _real_ code, i.e.
the code you are actually trying to compile.)

The error message is most likely caused by a bug in your compiler.
According to 11/5 access control is not supposed to restrict access to
'A::size' in the above member definition.

Sorry. That was some code I made up. I was trying to define a static array
of pointers-to-member functions of the class, and the size of this array
is a static const int. Apparently, my syntax was wrong. As suggested in
C++ FAQ, I used a typedef for the pointer-to-member type, and it compiled
correctly. Thanks.

Rahul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,126
Messages
2,570,753
Members
47,313
Latest member
SamualGriz

Latest Threads

Top