First look at the following code:
int main () {
Double *p;
printf ("sizeof (int):%d\nsizeof (Double):%d\nsizeof (PTR):%d\n", sizeof (int), sizeof (double), sizeof (p));
GetChar ();
return 0;
}
The result is:
I am running on a 64-bit machine and can see that the int type is assigned 4 bytes, a double type of 8 bytes, and a pointer of 4 bytes.
/**********************/
Then look at the following code:
/***************** Empty Class ****************/
#include <iostream>
Class a{
};
int main () {
A;
printf ("sizeof (a)%d\nsizeof (a)%d\n", sizeof (a), sizeof (a));
GetChar ();
return 0;
}
The result is:
The/*********** class has an int type, and ***********/
Class a{
int n;
};
/******* with Pointers ********/
Class a{
int *n;
};
/******* has a function *******/
Class a{
void Fun () {}
};
/****** Enumeration Type ****/
Class a{
Enum
{white = 1};
};
So what's the nesting class like?
Class a{
Public:
Class b{
int n;
int *p;
};
};
The result is still
This means that nested classes are not stored as pointers.
But if you write this:
Class a{
Public:
Class b{
int n;
int *p;
};
b b;
};
You'll find that he's exactly equal to the size of B, but there's a difference. Class B is the defining part of the classes that can be written like this
Class a{
Public:
Class B;
B *b;//no prior to declaring only references or pointers
};
Class a::b{
int n;
int *p;
};
Result is
This is the size of the pointer.
b b Creates a real instance of the class to allocate space in type B, which is no longer called a nested class but is called a containing class.
So, however, in storage space, nested classes are not stored with instance variables, but are stored in the same way as enumeration types, functions, and even pointers are not counted.
The storage effect is the same as the following, and the difference is that the hierarchical relationship of the access has changed.
Class b{
int n;
int *p;
};
Class a{
Public:
b b;
};
When nested classes and enumerations are defined in the public section are
It can be accessed externally in the form of a::b. This is like a type that will not be shared by all instance objects.
C + + Nested classes-memory allocation