Initialize an static array in a class

R

RCAJ

How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.
 
L

lallous

Hello,

RCAJ said:
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.
Static variables should be initialized from outside class.
 
J

Jeff Schwab

RCAJ said:
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.

Yeah, those up-tight compilers can be really retentive, you know?


class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };
 
A

Alexandros

Static variables should be initialized from outside class.

how?

I just want const values for that array. How can I initialize it so that
when I create an object the values are ready to be used without
initializing them in the constructor?
 
A

Alexandros

Jeff Schwab escribió:
RCAJ said:
How can I do the following?

class C{
private:
static const char c[3] = {1,2,3};
//...
};

The compiler says it can't be done like that.


Yeah, those up-tight compilers can be really retentive, you know?


class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };

Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?
 
V

Victor Bazarov

Alexandros said:
[...]
Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?

Probably because it's _private_...
 
J

Jeff Schwab

Alexandros said:
class C{
private:
static const char c[3] = {1,2,3};
//...
};
class C
{
static const char c[ 3 ];
};

const char C::c[ 3 ] = { 1, 2, 3 };

Thanks. It already works! But I've got a question: why can I get access
to a private atribute outside the class?

You're not really accessing it, you're still defining and initializing
it. The semantics are the same as for any private method. You can
define it outside the class. The defintion doesn't count as an access.
You're not actualling calling the method, or in this case, accessing
the variable.

-Jeff
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top