473,471 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why static data members can be declared as the type of its own class

Hello Experts,
Why static data members can be declared as the type of class which it
belongs to?

Inside a class, non-static data members such as pointers and references
can be declared as type of its own class. Non-static data members can
not be declared as type of its own class (excluding pointers and
references).

Why static data members can be declared as the type of its own class,
even though this member is not pointer or reference?

What's the consideration behind the restrictions or behavior of the
language? Thanks!
class Date{
static Date default_date;

public:
Date(int dd=0, int mm=0, int yy=0);
};

Date Date::default_date(16, 12, 1770);
-- lovecreatesbeauty

Aug 24 '05 #1
6 2449
lovecreatesbeauty wrote:
Hello Experts,
Why static data members can be declared as the type of class which it
belongs to?

Inside a class, non-static data members such as pointers and
references can be declared as type of its own class. Non-static data
members can not be declared as type of its own class (excluding
pointers and references).

Why static data members can be declared as the type of its own class,
even though this member is not pointer or reference?

What's the consideration behind the restrictions or behavior of the
language? Thanks!
class Date{
static Date default_date;

public:
Date(int dd=0, int mm=0, int yy=0);
};

Date Date::default_date(16, 12, 1770);


Let's say, you were allowed to declare a non-static member of the same
type as the class. What would be the size of an instance of such class?

Now, once you figure out why it's impossible to create an instance of
a class that has a member of the same type, you might deduce that the
other variations (a pointer or a reference or a static member) do not
represent the same size problem. Pointers or references if take up any
space, it's limited, static data members do not take up any space in
an instance of the class.

V
Aug 24 '05 #2
But a static data member (of the same type as the class) will take up
space always. When define a static data member of the same type as the
class, the class itself is incomplete, how to calculate the size of the
space, and why the members of this static can be referenced? Thank you!

-- lovecreatesbeauty

Aug 24 '05 #3
lovecreatesbeauty wrote:
But a static data member (of the same type as the class) will take up
space always.
Yes, but not in each instance of the class. Static data members exist
in their one-per-class uniqueness.
When define a static data member of the same type as the
class, the class itself is incomplete, how to calculate the size of
the space, and why the members of this static can be referenced?


The size of the class is the sum of the sizes its base class sub-objects,
and its non-static data, with some additions like padding and supplemental
elements (all implementation-defined) like vtbl, virtual base class ptr.
Static data members do NOT participate in the calculation of the final
size of the class instance.

When you declare a static data member in a class, it's a declaration, no
memory is allocated untill the _definition_ is encountered, and it is
_always_ outside (i.e. after) the class definition, and at that time the
class is complete. Static data members can be referenced just like
member functions, for example, for which only a declaration is required
(a definition will be required to complete the program).

You may think of static data members like of global data whose scope is
limited to their class and who also has access rights defined for them.
Aside from scope and access (that actually only have meaning while the
code is being compiled, anyway), the static data members are nothing but
global data.

V
Aug 24 '05 #4
Victor Bazarov wrote:
[snip]
You may think of static data members like of global data whose scope is
limited to their class and who also has access rights defined for them.
Aside from scope and access (that actually only have meaning while the
code is being compiled, anyway), the static data members are nothing but
global data.


I always use a little toy image to keep the functionality
of this straight. Consider a car factory and the cars it
produces. Information about the factory that each car needs
to know would be static. Information about each specific
car would be non-static.

So, the location of the factory is static, and you might
need to know that in order to make use of your warranty.
The location of each specific car is a property of that
car that can change, and is non-static.

So a "car location" class might include as static data
the location the car was built.
Socks

Aug 24 '05 #5
"lovecreatesbeauty" <lo***************@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Hello Experts,
Why static data members can be declared as the type of class which it
belongs to?

Inside a class, non-static data members such as pointers and references
can be declared as type of its own class. Non-static data members can
not be declared as type of its own class (excluding pointers and
references).

Why static data members can be declared as the type of its own class,
even though this member is not pointer or reference?

What's the consideration behind the restrictions or behavior of the
language? Thanks!
class Date{
static Date default_date;

public:
Date(int dd=0, int mm=0, int yy=0);
};

Date Date::default_date(16, 12, 1770);
-- lovecreatesbeauty


A static data member takes up sizeof(class) bytes.

If a class could contain an instance of it's own class, it's size would be
infinite.

See, say your Date class could contain a Date class member. This Date class
member would also, of course, contain a Date class member, which would
include a Data class member, which would include a Data class member, ad
nausium. Infinite regression just took place.

A pointer on the other hand takes a finite size. You could infintly regress
it easily but that wouldnt' happen til run time.

class infinity
{
private:
infinity* moreinfinity;
public:
infinity() { moreinfnity = new infinity; };
};

Meh, I might try to get that to compile and run just to watch it crash.
Sep 2 '05 #6
Jim Langston wrote:
ldnt' happen til run time.

class infinity
{
private:
infinity* moreinfinity;
public:
infinity() { moreinfnity = new infinity; };
};

Meh, I might try to get that to compile and run just to watch it crash.


[PEDANTIC and HUMOR]
You really should use an auto_ptr<infinity> there, otherwise your lack
of destructor will cause a resource leak :) Also, you should use an
initializer list :)
[/PEDANTIC and HUMOR]
Sep 2 '05 #7

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
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:...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
3
by: pauldepstein | last post by:
Sorry in advance if this message sounds imprecise but it's difficult to be precise when you don't really understand what's going on. I have a class called Parameters. The default constructor...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.