Pre-declarations in C++.

Forward declaration is occasionally required when writing C++ programs.In the following program, the commented line is t...

Forward declaration is occasionally required when writing C++ programs.In the following program, the commented line is the preceding description of class B.This is necessary because class B is used in class A, and the declaration of class B appears after class A.Without a pre-description of class B, the following program will be compiled differently and the compiler will give an error hint such as "Missing type specifier".
Code 1:

// ForwardDeclaration.h #include <iostream> using namespace std; class B; // This is a Forward declaration class A { private: B* b; public: A(B* b):b(b) { } ... }; class B { ... }; // Main.cpp #include "ForwardDeclaration.h" int main(int argc, char** argv) { B* b = new B(); A* a = new A(b); delete a; delete b; return 0; }

The above program compiles and runs smoothly (with little to do and no output).

Is everything going to be great with the preceding instructions?Let's look at the following code (the shaded part is new):
Code 2:

// ForwardDeclaration.h #include <iostream> using namespace std; class B; // This is a Forward declaration class A { private: B* b; public: A(B* b):b(b) { } void someMethod() { b->someMethod(); // (1) } }; class B { private: public: void someMethod() { cout << "something happened..." << endl; } }; // Main.cpp #include "ForwardDeclaration.h" int main(int argc, char** argv) { B* b = new B(); A* a = new A(b); a->someMethod(); delete a; delete b; return 0; }

Once compiled, an error was found in code (1).Error hints often include (different compilers give different hints):
1. Use undefined type B;
2. The left side of'->somemethod'must point to class/structure/union/generic type

Reason:
1. (1) The definition of type B is used because a member function in class B is called.Class B is declared before; only one type like B is declared, but no related definition is given. The related definition of class B occurs after class A, so a compilation error occurs.
2. Code 1 can be compiled because it only uses the type B, not the definition of class B.

What is the solution?
Separates the declaration of a class from its implementation (that is, the definition of the class).As follows:

// Declaration of the ForwardDeclaration.h class #include <iostream> using namespace std; class B; // This is a Forward declaration class A { private: B* b; public: A(B* b); void someMethod(); }; class B { private: 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* a = new A(b); a->someMethod(); delete a; delete b; return 0; }

Conclusion:
Pre-declarations can only be used as pointers or references, cannot define objects of classes, and naturally cannot call methods in objects.

Also note that if class A member variable B*b; is overridden to B&b; then B must be initialized in class A's constructor using an initialization list, otherwise errors will occur.For more information on this, see: Initialization of member variables for special data types

From: http://patmusing.blog.163.com/blog/static/135834960201038113714199/

3 July 2020, 12:15 | Views: 5984

Add new comment

For adding a comment, please log in
or create account

0 comments