473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Nov 7 '07 #1
3 1077
su**************@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
Nov 7 '07 #2
su**************@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.
Nov 7 '07 #3
su**************@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
Nov 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
1
by: Frederiek | last post by:
Hi, When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. Does that mean that the address of...
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
10
by: shanknbake | last post by:
I'm getting the following compile-time error: error C2352: 'Person::getCount' : illegal call of non-static member function Here is my getCount function declaration:...
6
by: subramanian100in | last post by:
why can't a static member function be declared as const ? We can declare a non-static member function as const, to indicate that it does not modify the non-mutable data members. In the same way, is...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.