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

L

lovecreatesbeauty

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
 
V

Victor Bazarov

lovecreatesbeauty said:
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
 
L

lovecreatesbeauty

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
 
V

Victor Bazarov

lovecreatesbeauty said:
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
 
P

Puppet_Sock

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
 
J

Jim Langston

lovecreatesbeauty said:
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.
 
R

red floyd

Jim said:
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]
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,824
Latest member
Nater888

Latest Threads

Top