Static data member initialization

Discussion in 'C++' started by Sam, Apr 16, 2005.

  1. Sam

    Sam Guest

    Hi,
    I need to initialize a static data member by calling a method of another
    static member. Like:

    class A
    {
    public:
    static int TYPE;
    private
    static Type _s_type;
    };

    int A::TYPE = _s_type.ID();
    Type A::_s_type;

    I compiled the code with my compiler. But I'm not sure whether it complies
    with the standard.

    Thanks.
    Sam
    Sam, Apr 16, 2005
    #1
    1. Advertising

  2. Sam wrote:
    > Hi,
    > I need to initialize a static data member by calling a method of
    > another static member. Like:
    >
    > class A
    > {
    > public:
    > static int TYPE;
    > private
    > static Type _s_type;
    > };
    >
    > int A::TYPE = _s_type.ID();
    > Type A::_s_type;
    >
    > I compiled the code with my compiler. But I'm not sure whether it
    > complies with the standard.


    I complies in a sense that it's well-formed. However, it has undefined
    behaviour because when you try to initialise A::TYPE, A::_s_type, which
    you need, hasn't been initialised properly yet. Reorder their definitions.

    V
    Victor Bazarov, Apr 17, 2005
    #2
    1. Advertising

  3. Sam

    Sam Guest

    > I complies in a sense that it's well-formed. However, it has undefined
    > behaviour because when you try to initialise A::TYPE, A::_s_type, which
    > you need, hasn't been initialised properly yet. Reorder their
    > definitions.
    >
    > V

    Thank you very much for your reply. Do you mean that it works well as long
    as _s_type is defined before TYPE?
    Type A::_s_type;
    int A::TYPE = _s_type.ID();
    I was wondering what the standard says about this.

    Sam
    Sam, Apr 17, 2005
    #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. Mike - EMAIL IGNORED

    static data member initialization

    Mike - EMAIL IGNORED, Feb 9, 2006, in forum: C++
    Replies:
    3
    Views:
    313
    Mike - EMAIL IGNORED
    Feb 11, 2006
  2. dolphin
    Replies:
    3
    Views:
    1,323
    Pete Becker
    Dec 5, 2007
  3. mathieu
    Replies:
    7
    Views:
    685
    James Kanze
    Feb 12, 2008
  4. Replies:
    23
    Views:
    844
    James Kanze
    Jun 15, 2008
  5. aaragon
    Replies:
    2
    Views:
    604
    James Kanze
    Nov 2, 2008
Loading...

Share This Page