| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"India" |
| Date: |
07 Nov 2007 05:33:41 AM |
| Object: |
question on static member data |
consider the following program:
class Test
{
public:
static Test i;
static double d;
int x;
};
Test t;
int main()
{
return 0;
}
This program compiles fine with both g++ and VC++ 2005 Express
Edition. Since I have not used the static data members I do not get
compilation error. My question is, why the compiler doesn't give an
error for not defining the static data members ?
Kindly explain.
Thanks
V.Subramanian
.
|
|
| User: "Michael DOUBEZ" |
|
| Title: Re: question on static member data |
07 Nov 2007 06:32:24 AM |
|
|
subramanian100in@yahoo.com, India a écrit :
consider the following program:
class Test
{
public:
static Test i;
static double d;
int x;
};
Test t;
int main()
{
return 0;
}
This program compiles fine with both g++ and VC++ 2005 Express
Edition. Since I have not used the static data members I do not get
compilation error. My question is, why the compiler doesn't give an
error for not defining the static data members ?
Because they are not required to do it.
The error comes from the link phase, not the compilation one. Since the
compilation doesn't require a link with the actual content of the
statics (because they are no used), everything if fine from the linker
point of view.
Michael
.
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: question on static member data |
07 Nov 2007 03:34:05 PM |
|
|
subramanian100in@yahoo.com, India wrote:
...
Since I have not used the static data members I do not get
compilation error. My question is, why the compiler doesn't give an
error for not defining the static data members ?
...
You have already answered your own question. In C++ you can declare
something and still keep it undefined as long as you are not using it in
the way that'd require a definition.
--
Best regards,
Andrey Tarasevich
.
|
|
|
|
| User: "Ron Natalie" |
|
| Title: Re: question on static member data |
07 Nov 2007 06:34:45 AM |
|
|
subramanian100in@yahoo.com, India wrote:
consider the following program:
class Test
{
public:
static Test i;
static double d;
int x;
};
Test t;
int main()
{
return 0;
}
This program compiles fine with both g++ and VC++ 2005 Express
Edition. Since I have not used the static data members I do not get
compilation error. My question is, why the compiler doesn't give an
error for not defining the static data members ?
You don't use them anywhere.
.
|
|
|
|

|
Related Articles |
|
|