static const Class Member

Discussion in 'C++' started by Alexander Hans, Sep 6, 2007.

  1. Hi,

    what's the usual way in C++ to define a static constant class member?
    Consider the following example:

    class TestClass
    {
    public:
    static const double a = 12.5;
    static const double b = 2 * a;
    };

    While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:

    > test.cpp:5: error: ‘TestClass::a’ cannot appear in a constant-expression



    I can't see where's the problem here. TestClass::a is declared as being
    constant and so is TestClass::b. I know that another possible way (that
    also g++-4.1.1 accepts) is to initialize the values outside the class
    definition:

    class TestClass
    {
    public:
    static const double a;
    static const double b;
    };

    const double TestClass::a = 12.5;
    const double TestClass::b = 2 * TestClass::a;

    However, I shouldn't put the last two lines into a header file if it is
    included by more than one .cpp implementation files if the resulting object
    files are supposed to be linked afterwards, since there will be multiple
    initializations then, no matter if they are the same or not. Putting the
    initialization into one specific implementation file, everything will work,
    but I would like to have the initialization within the header file.

    Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
    telling me why TestClass::a can't appear in a constant-expression.


    Best regards,

    Alex
     
    Alexander Hans, Sep 6, 2007
    #1
    1. Advertisements

  2. Alexander Hans

    Barry Guest

    Alexander Hans wrote:
    > Hi,
    >
    > what's the usual way in C++ to define a static constant class member?
    > Consider the following example:
    >
    > class TestClass
    > {
    > public:
    > static const double a = 12.5;
    > static const double b = 2 * a;


    only integral type can't be initialized inside of class declaration.

    > };
    >
    > While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
    >
    >> test.cpp:5: error: ‘TestClass::a’ cannot appear in a constant-expression

    >
    >
    > I can't see where's the problem here. TestClass::a is declared as being
    > constant and so is TestClass::b. I know that another possible way (that
    > also g++-4.1.1 accepts) is to initialize the values outside the class
    > definition:
    >
    > class TestClass
    > {
    > public:
    > static const double a;
    > static const double b;
    > };
    >
    > const double TestClass::a = 12.5;
    > const double TestClass::b = 2 * TestClass::a;
    >
    > However, I shouldn't put the last two lines into a header file if it is
    > included by more than one .cpp implementation files if the resulting object


    you need to place them in a cxx file.

    > files are supposed to be linked afterwards, since there will be multiple
    > initializations then, no matter if they are the same or not. Putting the
    > initialization into one specific implementation file, everything will work,
    > but I would like to have the initialization within the header file.
    >
    > Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
    > telling me why TestClass::a can't appear in a constant-expression.
    >
    >
    > Best regards,
    >
    > Alex
    >



    --
    Thanks
    Barry
     
    Barry, Sep 6, 2007
    #2
    1. Advertisements

  3. Alexander Hans

    blangela Guest

    On Sep 6, 7:53 am, Barry <> wrote:
    > Alexander Hans wrote:
    > > Hi,

    >
    > > what's the usual way in C++ to define a static constant class member?
    > > Consider the following example:

    >
    > > class TestClass
    > > {
    > > public:
    > > static const double a = 12.5;
    > > static const double b = 2 * a;

    >
    > only integral type can't be initialized inside of class declaration.
    >
    >
    >
    >
    >
    > > };

    >
    > > While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:

    >
    > >> test.cpp:5: error: 'TestClass::a' cannot appear in a constant-expression

    >
    > > I can't see where's the problem here. TestClass::a is declared as being
    > > constant and so is TestClass::b. I know that another possible way (that
    > > also g++-4.1.1 accepts) is to initialize the values outside the class
    > > definition:

    >
    > > class TestClass
    > > {
    > > public:
    > > static const double a;
    > > static const double b;
    > > };

    >
    > > const double TestClass::a = 12.5;
    > > const double TestClass::b = 2 * TestClass::a;

    >
    > > However, I shouldn't put the last two lines into a header file if it is
    > > included by more than one .cpp implementation files if the resulting object

    >
    > you need to place them in a cxx file.
    >
    > > files are supposed to be linked afterwards, since there will be multiple
    > > initializations then, no matter if they are the same or not. Putting the
    > > initialization into one specific implementation file, everything will work,
    > > but I would like to have the initialization within the header file.

    >
    > > Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
    > > telling me why TestClass::a can't appear in a constant-expression.

    >
    > > Best regards,

    >
    > > Alex

    >
    > --
    > Thanks
    > Barry- Hide quoted text -
    >
    > - Show quoted text -


    I think Barry meant to say only integral types CAN be initialized
    inside a class declaration. This includes char, short, int, long int,
    long long and enum types. It does _NOT_ include float, double or long
    double.
     
    blangela, Sep 6, 2007
    #3
  4. Alexander Hans

    Barry Guest

    blangela wrote:
    > On Sep 6, 7:53 am, Barry <> wrote:
    >> Alexander Hans wrote:
    >>> Hi,
    >>> what's the usual way in C++ to define a static constant class member?
    >>> Consider the following example:
    >>> class TestClass
    >>> {
    >>> public:
    >>> static const double a = 12.5;
    >>> static const double b = 2 * a;

    >> only integral type can't be initialized inside of class declaration.
    >>
    >>
    >>
    >>
    >>
    >>> };
    >>> While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
    >>>> test.cpp:5: error: 'TestClass::a' cannot appear in a constant-expression
    >>> I can't see where's the problem here. TestClass::a is declared as being
    >>> constant and so is TestClass::b. I know that another possible way (that
    >>> also g++-4.1.1 accepts) is to initialize the values outside the class
    >>> definition:
    >>> class TestClass
    >>> {
    >>> public:
    >>> static const double a;
    >>> static const double b;
    >>> };
    >>> const double TestClass::a = 12.5;
    >>> const double TestClass::b = 2 * TestClass::a;
    >>> However, I shouldn't put the last two lines into a header file if it is
    >>> included by more than one .cpp implementation files if the resulting object

    >> you need to place them in a cxx file.
    >>
    >>> files are supposed to be linked afterwards, since there will be multiple
    >>> initializations then, no matter if they are the same or not. Putting the
    >>> initialization into one specific implementation file, everything will work,
    >>> but I would like to have the initialization within the header file.
    >>> Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
    >>> telling me why TestClass::a can't appear in a constant-expression.
    >>> Best regards,
    >>> Alex

    >> --
    >> Thanks
    >> Barry- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > I think Barry meant to say only integral types CAN be initialized
    > inside a class declaration. This includes char, short, int, long int,
    > long long and enum types. It does _NOT_ include float, double or long
    > double.
    >


    Yep,
    sorry, kind of merge "you can't ...", "..." together. :)

    --
    Thanks
    Barry
     
    Barry, Sep 7, 2007
    #4
    1. Advertisements

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. E11
    Replies:
    1
    Views:
    5,757
    Thomas Weidenfeller
    Oct 12, 2005
  2. Siemel Naran
    Replies:
    4
    Views:
    1,064
    Micah Cowan
    Jan 12, 2005
  3. Rakesh Sinha
    Replies:
    4
    Views:
    2,149
    Rakesh Sinha
    Jan 13, 2005
  4. Dave
    Replies:
    10
    Views:
    35,838
    Ron Natalie
    May 22, 2005
  5. Javier
    Replies:
    2
    Views:
    906
    James Kanze
    Sep 4, 2007
  6. dolphin
    Replies:
    3
    Views:
    1,629
    Pete Becker
    Dec 5, 2007
  7. er
    Replies:
    3
    Views:
    491
  8. fungus
    Replies:
    13
    Views:
    1,235
    fungus
    Oct 31, 2008
Loading...