question on static member data

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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
 
M

Michael DOUBEZ

(e-mail address removed), 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
 
R

Ron Natalie

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.
 
A

Andrey Tarasevich

...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top