CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    326

    static member initialization

    Hi,

    My understanding was that all static data members in a class needs to be initialized.

    In one of the programs, I forgot to initialize a static composite data member (what i mean by composite type is a a class type) and the compiler didn't throw any error.

    It did throw an error when i didn't initialize a static primitive data member

    Code:
    class A
    {};
    
    class B
    {
        public:
            static void f1();
        private:
            static int n1;
            static A a1;
    };
    
    
    void B :: f1()
    {
        n1 = 10; //throws an error
        A a2;
        a2 = a1; //does not throw an error though it is not initialized
    }
    
    int main()
    {
        return(0);
    }
    error:
    Code:
    hyper71: singleton> g++ test.cpp -o test
    /tmp/cciEM614.o: In function `B::f1()':
    test.cpp:(.text+0x6): undefined reference to `B::n1'
    collect2: ld returned 1 exit status

    Questions:
    ========
    1) Are the static composite (class type) data members initialized using their default constructors ? And is that the reason why the compilers don't throw an error.

    2) The error thrown is "undefined reference to `B::n1'". Not sure i fully understand the error message, I thought it should have thrown an error related to not initializing.

    3) Is class data type referred as composite data types ? - not sure if there is another term for this ...

    Thanks,
    Muthu

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: static member initialization


  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: static member initialization

    Static member variables need to be "defined" , not initialized. That is why you get
    an undefined reference.

    I would think that both of the static variables should have given the undefined
    reference error. Try actually using the function ...

    Code:
    int main()
    {
        B b;
        b.f1();
        
        return(0);
    }

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: static member initialization

    Thanks Skizmo and Philip, I am just a bit confused with definition vs initialization.

    The reason I am confused is that because the below mentioned program compiles successfully (a1 is not defined / initialized):

    I have pasted the complete code just to be sure - gcc version 4.2.1

    Code:
    class A
    {};
    
    class B
    {
        public:
            static void f1();
        private:
            static int n1; 
            static A a1; 
    };
    
    
    void B :: f1()
    {
        //n1 = 10; //throws an error
        A a2; 
        a2 = a1; //does not throw an error though it is not initialized
    }
    
    int main()
    {
        B b;
        b.f1();
    
        return(0);
    }
    Last edited by Muthuveerappan; July 12th, 2011 at 09:47 AM.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: static member initialization

    Quote Originally Posted by Muthuveerappan
    The reason I am confused is that because the below mentioned program compiles successfully (a1 is not defined / initialized):
    You're probably observing the effects of undefined behaviour.

    If I uncomment the "throws an error" and compile with the Comeau online compiler, the program compiles successfully. If I add an int member variable to A, Comeau still does not complain. But g++ then fails to compile the program.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured