473,288 Members | 1,718 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,288 software developers and data experts.

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 2434
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?
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...

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.