Thread: Initilization of static const string

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Initilization of static const string

    Code:
    FileA.h
    class A{
    public:
      static const int x;
      static const string str;
    ...
    };
    What's the difference to initilize a static const int and a static const string?
    1. Can I initialize static const string str("abc") in the body of class A? Can i do the same for x?
    2. Can I initialize const string str("abc") in the header file but outof class A? how about x?
    3. Shall I use = "abc" instead of ("abc") ?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can initialize a static const int (or any other integral type like char, short, long or bool) inside the class definition if you want.

    Otherwise, you should define the static const variable outside of the class definition and inside a source file. If you did it in a header file you would likely get multiple definition errors.

    You can use = "abc" or ("abc"), either will work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM