Aaaargh! noob question static string array initialization

Discussion in 'C++' started by anti-guru, Sep 1, 2004.

  1. anti-guru

    anti-guru Guest

    This is killing me. How can I make a static array of two strings in my
    class?

    Nothing I've tried works. Here is what I currently have, which also doesn't
    work, but gives the least amount of compiler errors:

    in my header file:

    private:
    static char *stringTable[ 2 ];

    in my class source file:

    *stringTable[ 0 ] = new char[ strlen( "myString" )];

    This particular attempt says "cannot convert from 'char *' to 'char'

    Ideally, I would like to use curly brackets to initialize everything at
    once. I've tried to do that in the header, but I've learned that it doesn't
    allow that.

    I've consulted my Deitel book, and my c++ primer, but I can't seem to put
    the pieces together.

    If it's not too much to ask, could you please not use any STL if you reply;
    I'm barely treading water with the regular language.

    Thanks!
    anti-guru, Sep 1, 2004
    #1
    1. Advertising

  2. "anti-guru" <> wrote in message
    news:iJrZc.71955$...
    > This is killing me. How can I make a static array of two strings in my
    > class?
    >
    > Nothing I've tried works. Here is what I currently have, which also
    > doesn't
    > work, but gives the least amount of compiler errors:
    >
    > in my header file:
    >
    > private:
    > static char *stringTable[ 2 ];
    >
    > in my class source file:
    >
    > *stringTable[ 0 ] = new char[ strlen( "myString" )];
    >
    > This particular attempt says "cannot convert from 'char *' to 'char'
    >
    > Ideally, I would like to use curly brackets to initialize everything at
    > once. I've tried to do that in the header, but I've learned that it
    > doesn't
    > allow that.
    >


    Assuming your class is called MyClass.

    char* MyClass::stringTable[2] = { "mystring1", "myString2" };

    Apart from the notation MyClass::stringTable this is exactly the same as the
    initialisation of any static array.

    john
    John Harrison, Sep 1, 2004
    #2
    1. Advertising

  3. anti-guru

    anti-guru Guest

    Thank you! :)



    >
    > Assuming your class is called MyClass.
    >
    > char* MyClass::stringTable[2] = { "mystring1", "myString2" };
    >
    > Apart from the notation MyClass::stringTable this is exactly the same as

    the
    > initialisation of any static array.
    >
    > john
    >
    >
    anti-guru, Sep 2, 2004
    #3
    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. toton
    Replies:
    5
    Views:
    919
    Victor Bazarov
    Sep 28, 2006
  2. Replies:
    23
    Views:
    838
    Chris Thomasson
    Aug 29, 2007
  3. krishna
    Replies:
    6
    Views:
    305
    James Kanze
    Sep 24, 2008
  4. aaragon
    Replies:
    2
    Views:
    605
    James Kanze
    Nov 2, 2008
  5. kj
    Replies:
    13
    Views:
    541
    Singletoned
    Nov 3, 2009
Loading...

Share This Page