Example of a C + + class declaration class predecessor declaration

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/staring-hxs/p/3244251.html

In the writing of C + + programs, it is occasionally necessary to use the predecessor Declaration (Forward Declaration). In the following program, the annotated line is the predecessor of Class B. This is necessary because class B is used in Class A, and the Declaration of Class B appears after Class A. If there is no predecessor to Class B, the following program will be different by compiling, and the compiler will give you an error message similar to the "Missing type descriptor".

Code one://Forwarddeclaration.h#include <iostream>namespace std;  Class B;             //This is the predecessor statement (Forward Declaration)class a{private:b* B;  Public:a (b* B): b (b) {}}; class b{}; "ForwardDeclaration.h"int main (return 0;}          

The program can be compiled and run smoothly (with little or no output).

Is there a front-of-the-place explanation? Let's take a look at the following code (the code with the shaded part is newly added):

Code two://forwarddeclaration.h#include <iostream>namespace std;  Class B;             //This is the predecessor statement (Forward Declaration)class a{private:b* B;  Public:a (b* B): B (b) {}
(1)}
}; class b{Public:
void SomeMethod ()
{ "something happened ..." << Endl; }
};     "ForwardDeclaration.h"int main (char** argv) {         new A (b); A->somemethod ();
Delete A;
Delete B;
return 0;}

A compilation error occurred at the discovery Code (1). Error hints often include (different compiler-given hints will vary):

1. Undefined type B is used;

2. The left side of "->somemethod" must point to class/struct/union/generic type

Reason:

1. (1) The definition of type B was used because a member function in Class B was called. Pre-declaration class B; Only a type of B is declared, and no relevant definition is given, and the related definition of Class B appears after Class A, resulting in a compilation error;

2. The code is compiled because it uses only the B type and does not use the definition of Class B.

What is the solution?

Separates the declaration of the class from the implementation of the class (that is, the definition of the class). As shown below:

Declaration of the ForwardDeclaration.h class # include <iostream>Usingnamespace Std;Class B;This is the predecessor statement (Forward Declaration)class a{private:b* b; Public:a (b* B); void SomeMethod ();}; class b{public:void SomeMethod ();}; //ForwardDeclaration.cpp class implementation # include  "ForwardDeclaration.h" a::a (b* B): b (b) {} 
void A::somemethod () {B->somemethod ();} void B::somemethod () {cout << "something happened ..." << Endl;} //main.cpp#include "ForwardDeclaration.h" int Main (int argc, char** argv) {b* b = new B ();     * A = new A (b); A->somemethod (); Delete A;    
Delete B; return 0;

Conclusion:

A predecessor declaration can only be used as a pointer or reference, and cannot define an object of a class, and it is not natural to call methods in the object.

It is also important to note that if you change the member variable of Class A b* B to b& B, then you must initialize B in the Class A constructor, with the initialization list, or else it will be an error. About this

See:

Initialization of special data type member variables

But my test is still not working.

Example of a C + + class declaration class predecessor declaration

Related Article

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.