The learning __c++ of the forward declaration of C + + class

Source: Internet
Author: User
2013-04-06 17:12:18 Tags: Beginners class C + + class forward declaration class contains original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, author information and this statement. Otherwise, legal liability will be held. http://liam2199.blog.51cto.com/2879872/1172175

http://liam2199.blog.51cto.com/2879872/1172175 original link

Suppose declaring two classes A and B, if you need to define Class B objects in A.h, b b_; is bound to include the # include "B.h", while class B needs to define object a a_; Also include B.h, but the mutual inclusions are not allowed, called circular references, when the forward declaration is used.

To make a forward declaration in Class B:

Class A;

There is no need to include A.h in B.h, but only if you cannot define an object, you can define only pointers or references.

Class A

A.h # ifndef _a_h_ # define _a_h_ # include ' B.h ' class A {a (void);            ~a (void);  B b_;    to include B.h}; # endif//_a_h

B.h

B.h # ifndef _b_h_ # define _B_H_/forward declaration, cannot instantiate object class A;      You don't need to include A.h, you can't define objects, you can only define pointers and references//Because we don't know what a class long declaration looks like, we can't allocate memory class B {B (void);        ~b (void);      void Fun (a& A)//can only be a pointer or reference {;  }//forward declared class cannot instantiate object * A_;    //  }; # endif//_b_h_

B.cpp

B.cpp # include "B.h" # include "A.h" b::b (void) {} b::~b () {}

Summarize:

A forward-declared class cannot instantiate an object.

Definition of a forward declaration: There are times when we can declare classes but not define them, and of course the role of this class is limited.

Like class Foo;

Declare an Foo class, this declaration, sometimes also called a forward declaration (Forward declaration), after declaring the Foo class, after defining the period before this Foo class, the Foo class is an incomplete type (incomplete type). That is, the Foo class is a type, but some of the properties of the type (such as which members are included and what operations are) are not known.

So the role of this class is also very limited.

(1) The object of the Foo class cannot be defined.

(2) can be used to define pointers or references that point to this type. (Very valuable things)

(3) Used to declare (not define) a function that uses the type as a formal parameter or a return type.

Because of the existence of a forward declaration, we can do something simple in many cases.

In C + +, if you want to write a header file for a class, you typically want to #include a bunch of header files, but most of them are not needed with the attributes of the forward declaration and C + + compiler.

C + + compiler to do the main thing is: 1. scan symbols; 2. Determines the object size.

So many times there is no need to include classes in.

Like what:

(1) Because all object type applications occupy the same space, the C + + compiler is good at confirming object size.

class string;

Class Sample

{

Private

String &s;

};

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.