question on static member data



 DEVELOP > c-Plus-Plus > question on static member data

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

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


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER