By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.