C + + Nested classes-memory allocation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.