Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialize an static array in a class

Reply
Thread Tools

Initialize an static array in a class

 
 
RCAJ
Guest
Posts: n/a
 
      12-27-2003
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.

 
Reply With Quote
 
 
 
 
lallous
Guest
Posts: n/a
 
      12-27-2003
Hello,

"RCAJ" <(E-Mail Removed)> wrote in message
newslgHb.2251322$(E-Mail Removed) s...
> 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.

--
Elias


 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      12-27-2003
RCAJ wrote:
> 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 };

 
Reply With Quote
 
Alexandros
Guest
Posts: n/a
 
      12-27-2003

> Static variables should be initialized from outside class.
>
> --
> Elias


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?



 
Reply With Quote
 
Alexandros
Guest
Posts: n/a
 
      12-28-2003
Jeff Schwab escribió:
> RCAJ wrote:
>
>> 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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-28-2003
"Alexandros" <(E-Mail Removed)> wrote...
> [...]
> 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_...


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      12-28-2003
Alexandros wrote:

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


<snip/>

>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize a static array with struct elements syang8 C++ 4 06-21-2008 09:32 AM
Elegant way to initialize non-static array member mathieu C++ 13 11-10-2007 02:28 PM
Elegant way to initialize non-static array member (2) mathieu C++ 2 11-08-2007 03:39 PM
How to initialize a 2D static array? Vol C Programming 1 03-10-2007 11:22 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM



Advertisments