|
-
July 12th, 2011, 04:46 AM
#1
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
-
July 12th, 2011, 06:21 AM
#2
Re: static member initialization
-
July 12th, 2011, 07:14 AM
#3
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);
}
-
July 12th, 2011, 09:44 AM
#4
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.
-
July 12th, 2011, 10:01 AM
#5
Re: static member initialization
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|