C ++ and other pre-statements

Source: Internet
Author: User
From:

Http://software.intel.com/zh-cn/blogs/2010/05/04/c-2/

 

People who are new to C ++ will encounter the following problems:

Define a Class A. This class uses Class B's object B, and then defines Class B, which also contains the object a of Class A, which becomes like this:

When compilation occurs, there is a problem of mutual inclusion. Someone jumps out and says, the solution to this problem can be like this, in. declare Class B in the H file, and then use the pointer of Class B.

Then the problem is solved.

However, does anyone know why the problem was solved? That is to say, the problem was solved by adding a pre-statement. Next, let me discuss this pre-declaration.

Class pre-declaration has many advantages.

One advantage of using the pre-declaration is that when we use the pre-declaration of Class B in Class A, when we modify Class B, we only need to re-compile Class B, instead of re-compiling. H (of course, when class B is actually used, it must contain B. h ).

Another benefit is to reduce the size of Class A, which is not reflected in the above Code. Let's look at it:

The code above shows that the size of Class B is 12 (on a 32-bit host ).

If Class A contains objects of Class B, the size of Class A is 12 (assuming there are no other member variables or virtual functions ). If the pointer * B Variable of Class B is included, the size of Class A is 4, which can reduce the size of Class, this is especially useful when STL containers contain class objects rather than pointers.

In the pre-declaration, we can only use the class pointer and reference (because the reference is also implemented by the pointer ).

So, I ask you a question: why can we only use type pointers and references in the pre-declaration?

If you answer: this is because the pointer is of a fixed size and can represent any type, you can get 80 points. Why is there only 80 points, because I haven't fully answered yet.

For more detailed answers, let's look at the following class:


Let's take a look at the Class A defined above, where the _ B variable and the get_ B () function are newly added to this class.

So I will ask you, after adding the _ B variable and the get_ B () member function, what changes have taken place in this class, and I will try again.

Let's list the changes:

The first change is, of course, adding the _ B variable and the get_ B () member function;

The second change is that the size of the class has changed. It turns out to be 4, and now it is 8.

The third change is that the offset address of member _ A has changed. The original offset relative to the class is 0, and now it is 4.

The above changes are all explicit and visible changes. There is also a hidden change. Think about it...

The hidden change is that the default constructor of Class A and the default copy constructor have changed.

From the above changes, we can see that the behavior of any member variable or member function that calls Class A needs to be changed. Therefore, our a.h needs to be re-compiled.

If our B .h is like this:

Then, we need to recompile B. H.

If so:

So we do not need to recompile B. H.

As we do, the pre-declaration class:

Class;

Is an incomplete declaration. As long as Class B does not execute operations that require understanding the size of class A or members, such an incomplete declaration allows the declaration of pointers and references to Class.

The statement in the previous Code

A;

It is necessary to understand the size of a, otherwise it is impossible to know if the memory size is allocated to Class B, so incomplete pre-declaration will not work, must include. h to obtain the size of Class A, and re-compile Class B.

Back to the preceding problem, the only allowed declaration using the pre-declaration is the pointer or reference. One reason is that as long as the declaration is not executed, you need to know the size of class A or the operation of the members, therefore, declaring a pointer or reference does not require understanding the size of class A or member operations.

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.