C ++ pre-Statement

Source: Internet
Author: User

 

Transferred from: http://blog.csdn.net/fjb2080/archive/2010/04/27/5533514.aspx Author: Qing Lin, blog name: Fei Kong Jing Du

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:

 

  1. // A. h
  2. # Include "B. H"
  3. Class
  4. {
  5. ....
  6. PRIVATE:
  7. B;
  8. };
  9. // B. H
  10. # Include "A. H"
  11. Class B
  12. {
  13. ....
  14. PRIVATE:
  15. A;
  16. };

 

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.

 

  1. // A. h
  2. // # Include "B. H"
  3. Class B;
  4. Class
  5. {
  6. ....
  7. PRIVATE:
  8. B;
  9. };
  10. // B. H
  11. # Include "A. H"
  12. Class B
  13. {
  14. ....
  15. PRIVATE:
  16. A;
  17. };

 

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:

 

  1. // A. h
  2. Class B;
  3. Class
  4. {
  5. ....
  6. PRIVATE:
  7. B * B;
  8. ....
  9. };
  10. // B. H
  11. Class B
  12. {
  13. ....
  14. PRIVATE:
  15. Int;
  16. Int B;
  17. Int C;
  18. };

 

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:

 

  1. Class
  2. {
  3. Public:
  4. A (int A): _ A (a), _ B (_ A) {}// _ B is new add
  5. Int get_a () const {return _ ;}
  6. Int get_ B () const {return _ B;} // new add
  7. PRIVATE:
  8. Int _ B; // new add
  9. Int _;
  10. };

 

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:

 

  1. // B. H
  2. # Include "A. H"
  3. Class B
  4. {
  5. ...
  6. PRIVATE:
  7. A;
  8. };

 

Then, we need to recompile B. H.

If so:

 

  1. // B. H
  2. Class;
  3. Class B
  4. {
  5. ...
  6. PRIVATE:
  7. A *;
  8. };

 

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.

 

 

From: http://blog.csdn.net/rogeryi/archive/2006/12/12/1439597.aspx

This article is largely inspired by Chapter 4 compiler firewils and the pimpl idiom in the predictional C ++ (hurb99, this chapter describes the meaning and usage of reducing dependencies during compilation. In fact, the most common and no side effect is to replace the header file with predeclaration.

Guideline-"never # include a header when a Forward Declaration will suffice" of item 26"

Here, I have summarized various situations that can be replaced by header files and provided some sample code.

First, why should we include header files? The answer to the question is simple. We usually need to get a definition of a certain type ). The next question is, under what circumstances do we need to define the type, and under what circumstances do we only need to declare it? The answer is that when we need to know the size of this type or the function signature, we need to get its definition.

Suppose we have types A and C, and under which circumstances does a need C:

  1. A inherits from C
  2. A has a member variable of type C.
  3. A has a member variable of type C pointer.
  4. A has a reference member variable of type C.
  5. A has a member variable of the STD: List <C> type.
  6. A has a function. The parameters and return values in its signature are of type C.
  7. A has a function. In its signature, parameters and return values are of type C. It calls a function of C and the code is in the header file.
  8. A has a function. Its signature parameters and return values are of type C (including type C itself, reference type C, and pointer type C ), and it will call another function using C, and the code will be written directly in the header file of.
  9. C and A are in the same namespace.
  10. C and A are in different namespaces.

1. There is no way to obtain the definition of C, because we must know the member variables and member functions of C.

2. We need the definition of C, because we need to know the size of C to determine the size of a, but we can use pimpl to improve this. For details, please
Look at hurb's exceptional C ++.

3, 4, no, the pre-declaration is enough. In fact, 3 and 4 are the same. The reference is also a pointer physically, and its size varies with the platform, it may be 32-bit or 64-bit. We can determine the size of this member variable without knowing the definition of C.

5. No. It is possible that the old-fashioned compiler needs it. Containers in the standard library are like list, vector, map,
The definition of C is not required when a list <C>, vector <C>, Map <C, C> type member variable is included. Because they actually use the C pointer as the member variable internally, their size is fixed at the beginning and will not change according to the template parameters.

6. No, as long as we do not use C.

7. Yes. We need to know the signature of the called function.

8, 8 is complex, and the code is clearer.

C & dotoc (C &);
C & dotoc2 (C & C) {return dotoc (c );};

From the code above, a member function dotoc2 of a calls another member function dotoc, but whether it is dotoc2 or dotoc, their Parameters and return types are actually references of C (change to pointer, and the situation is the same). The referenced value assignment is the same as the pointer value assignment, and it is nothing more than an integer value assignment, therefore, you do not need to know the size of C or call any function of C. In fact, the definition of C is not required here.

However, we can replace one of C & with C, for example, the following examples:

1.
C & dotoc (C &);
C & dotoc2 (C) {return dotoc (c );};

2.
C & dotoc (C );
C & dotoc2 (C & C) {return dotoc (c );};

3.
C dotoc (C &);
C & dotoc2 (C & C) {return dotoc (c );};

4.
C & dotoc (C &);
C dotoc2 (C & C) {return dotoc (c );};

Either method actually contains a copy constructor call. For example, parameter C in 1 is generated by the copy constructor, in 3, the return value of dotoc is an anonymous object generated by the copy constructor. Because we call the copy constructor of C, we need to know the definition of C in any of the above cases.

9 and 10 are the same. We do not need to know the definition of C, but in the case of 10, the syntax of the pre-declaration will be slightly more complicated.

Finally, a complete example is provided. We can see how the types A and C in two different namespaces Replace the header files directly including C with the pre-declaration:

A.h

# Pragma once

# Include <list>
# Include <vector>
# Include <map>
# Include <utility>

// Pre-declaration method for different namespaces
Namespace test1
{
Class C;
}



Namespace Test2
{
// Use using to avoid using a fully qualified name
Using test1: C;

Class
{
Public:
C USEC (C );
C & dotoc (C &);
C & dotoc2 (C & C) {return dotoc (c );};

PRIVATE:
STD: List <C> _ list;
STD: vector <C> _ vector;
STD: Map <C, C> _ map;
C * _ PC;
C & _ RC;

};
}

C. h

# Ifndef C_H
# Define C_H
# Include <iostream>

Namespace test1
{

Class C
{
Public:
Void print () {STD: cout <"Class C" <STD: Endl ;}
};

}

# Endif // C_H

 

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.