Initialize an static array in a class

Discussion in 'C++' started by RCAJ, Dec 27, 2003.

  1. RCAJ

    RCAJ Guest

    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.
     
    RCAJ, Dec 27, 2003
    #1
    1. Advertising

  2. RCAJ

    lallous Guest

    Hello,

    "RCAJ" <> wrote in message
    news:DlgHb.2251322$...
    > 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
     
    lallous, Dec 27, 2003
    #2
    1. Advertising

  3. RCAJ

    Jeff Schwab Guest

    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 };
     
    Jeff Schwab, Dec 27, 2003
    #3
  4. RCAJ

    Alexandros Guest


    > 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?
     
    Alexandros, Dec 27, 2003
    #4
  5. RCAJ

    Alexandros Guest

    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?
     
    Alexandros, Dec 28, 2003
    #5
  6. "Alexandros" <> 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_...
     
    Victor Bazarov, Dec 28, 2003
    #6
  7. RCAJ

    Jeff Schwab Guest

    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
     
    Jeff Schwab, Dec 28, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tarlika Elisabeth Schmitz

    accessing static constant does not initialize class

    Tarlika Elisabeth Schmitz, Dec 11, 2004, in forum: Java
    Replies:
    2
    Views:
    456
    Tarlika Elisabeth Schmitz
    Dec 12, 2004
  2. Vol

    How to initialize a 2D static array?

    Vol, Mar 10, 2007, in forum: C Programming
    Replies:
    1
    Views:
    427
    Keith Thompson
    Mar 10, 2007
  3. mathieu
    Replies:
    13
    Views:
    813
    terminator
    Nov 10, 2007
  4. mathieu
    Replies:
    2
    Views:
    557
    mathieu
    Nov 8, 2007
  5. syang8
    Replies:
    4
    Views:
    725
    James Kanze
    Jun 21, 2008
Loading...

Share This Page