Declaring constants within the scope of a class

Discussion in 'C++' started by Generic Usenet Account, Mar 15, 2005.

  1. I am trying to compile the following sample code:

    class WhatISHappeningHere
    {
    static const int x = 32;
    static const char* yy = "Howdy";
    // ...
    // blah blah blah
    // ...
    };


    My GCC compiler (g++ version 2.95.2) is giving the following misleading
    compiler error:

    ANSI C++ forbids in-class initialization of non-const static member
    `yy'


    I suspect that initialization of static data members that are not of an
    integral type is not allowed in C++, but the compiler is giving a wrong
    error message. Kindly confirm/refute my observation.

    Thanks,
    Gus
     
    Generic Usenet Account, Mar 15, 2005
    #1
    1. Advertising

  2. Generic Usenet Account

    Shezan Baig Guest

    Generic Usenet Account wrote:
    > I am trying to compile the following sample code:
    >
    > class WhatISHappeningHere
    > {
    > static const int x = 32;
    > static const char* yy = "Howdy";
    > // ...
    > // blah blah blah
    > // ...
    > };
    >
    >
    > My GCC compiler (g++ version 2.95.2) is giving the following

    misleading
    > compiler error:
    >
    > ANSI C++ forbids in-class initialization of non-const static member
    > `yy'
    >
    >
    > I suspect that initialization of static data members that are not of

    an
    > integral type is not allowed in C++, but the compiler is giving a

    wrong
    > error message. Kindly confirm/refute my observation.
    >
    > Thanks,
    > Gus




    I'm not sure if this will work, but try:

    static const char* const yy = "Howdy";
     
    Shezan Baig, Mar 15, 2005
    #2
    1. Advertising

  3. Generic Usenet Account

    Howard Guest

    "Shezan Baig" <> wrote in message
    news:...
    >
    > Generic Usenet Account wrote:
    >> I am trying to compile the following sample code:
    >>
    >> class WhatISHappeningHere
    >> {
    >> static const int x = 32;
    >> static const char* yy = "Howdy";
    >> // ...
    >> // blah blah blah
    >> // ...
    >> };
    >>
    >>
    >> My GCC compiler (g++ version 2.95.2) is giving the following

    > misleading
    >> compiler error:
    >>
    >> ANSI C++ forbids in-class initialization of non-const static member
    >> `yy'
    >>
    >>
    >> I suspect that initialization of static data members that are not of

    > an
    >> integral type is not allowed in C++, but the compiler is giving a

    > wrong
    >> error message. Kindly confirm/refute my observation.
    >>
    >> Thanks,
    >> Gus

    >
    >
    >
    > I'm not sure if this will work, but try:
    >
    > static const char* const yy = "Howdy";
    >


    That won't work, either. As the OP thought, you're not allowed to
    initialize static data members in the class definition, unless they're of
    integral type. They need to be initialized outside the class definition
    (and outside the header file, if you're using one, to avoid multiple
    definitions).

    Like you, I was thinking that the reason the compiler is saying that's
    "non-const" is that it's a const ponter, but to non-const data. But making
    it const data won't help here. It has to be an integral type to be
    initialized "in class".

    -Howard
     
    Howard, Mar 15, 2005
    #3
  4. Shezan Baig wrote:
    > Generic Usenet Account wrote:
    >
    >>I am trying to compile the following sample code:
    >>
    >>class WhatISHappeningHere
    >>{
    >> static const int x = 32;
    >> static const char* yy = "Howdy";
    >> // ...
    >> // blah blah blah
    >> // ...
    >>};
    >>
    >>
    >>My GCC compiler (g++ version 2.95.2) is giving the following

    >
    > misleading
    >
    >>compiler error:
    >>
    >>ANSI C++ forbids in-class initialization of non-const static member
    >>`yy'


    'yy' is definitely non-const. It would be 'const' if "const" were
    sitting right in front of him. See the declaration below (which is
    not going to work anyway).

    >>I suspect that initialization of static data members that are not of

    >
    > an
    >
    >>integral type is not allowed in C++, but the compiler is giving a

    >
    > wrong
    >
    >>error message. Kindly confirm/refute my observation.


    Seems like you're incorrect. However, the fact that you got confused
    by the message may allow qualifying the message as 'confusing', but not
    misleading.

    >>
    >>Thanks,
    >>Gus

    >
    >
    >
    >
    > I'm not sure if this will work, but try:
    >
    > static const char* const yy = "Howdy";


    This will NOT work. You are only allowed to initialise static const
    members of _integral_ types.

    V
     
    Victor Bazarov, Mar 15, 2005
    #4
  5. Howard wrote:
    > [..] As the OP thought, you're not allowed to
    > initialize static data members


    ....static _const_ data members...

    (Just to pick a nit)

    > in the class definition, unless they're of
    > integral type. They need to be initialized outside the class definition
    > (and outside the header file, if you're using one, to avoid multiple
    > definitions).
    > [..]



    V
     
    Victor Bazarov, Mar 15, 2005
    #5
  6. Generic Usenet Account

    Howard Guest

    "Victor Bazarov" <> wrote in message
    news:g7EZd.53329$01.us.to.verio.net...
    > Howard wrote:
    >> [..] As the OP thought, you're not allowed to initialize static data
    >> members

    >
    > ...static _const_ data members...
    >
    > (Just to pick a nit)
    >
    > > in the class definition, unless they're of
    >> integral type. They need to be initialized outside the class definition
    >> (and outside the header file, if you're using one, to avoid multiple
    >> definitions).
    >> [..]



    Quite correct. Thanks. (I hate those nits!)

    -Howard
     
    Howard, Mar 15, 2005
    #6
    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. Troy Knight

    declaring constants

    Troy Knight, Jul 25, 2004, in forum: Perl
    Replies:
    1
    Views:
    510
  2. Mortal Wombat
    Replies:
    3
    Views:
    6,867
    Mortal Wombat
    Aug 7, 2003
  3. Declaring constants

    , May 29, 2006, in forum: VHDL
    Replies:
    5
    Views:
    724
  4. Sriram Chadalavada

    Defining/declaring constants in Python

    Sriram Chadalavada, Jul 28, 2003, in forum: Python
    Replies:
    2
    Views:
    4,311
    Michael Hudson
    Jul 29, 2003
  5. Schizoid Man
    Replies:
    1
    Views:
    346
    Victor Bazarov
    Nov 2, 2006
Loading...

Share This Page