Problems with two classes referencing each other in C + +

Source: Internet
Author: User

Recently when changing a C + + program encountered a warning message, the warning message is: "

Remove pointers to incomplete "q2dtorusnode" types; destructors are not called
1> c:\users\lxw\desktop\dragonfly Second stage experiment \ Final experimental version \ Experiment catalogue \DRAGONFLY_MODIFY\SRC\Q2DTORUS.H (6): See Declaration of "Q2dtorusnode"

The warning message is very strange, in fact, because of obsessive-compulsive disorder to resolve the warning message, and from the warning message, the program should also have a memory leak problem, because the warning directly to tell you, did not call the destructor, and then I resolved the process. I will set up a simple program to simulate this error, because the program is in some more ~

source of the warning:

A header file A.h contains the code for Class A as follows:

#ifndef ah#define ahclass b;class A {B *memb; A () {}~a () {delete memb;}}; #endif

A header file B.h contains the code for Class B as follows:

#ifndef bh#define bhclass B {    }; #endif

At this point the compilation produces a warning message similar to the above: Warning C4150: Deletes a pointer to an incomplete "B" type, and no destructor is called.

cause Analysis:

Because the declaration of B in Class A relies on the predecessor Declaration of Class B, rather than the # include "B.H", the definition of B is not visible to a, so destructors cannot be called, resulting in memory leaks.

changes in the program

What happens if Class A and Class B keep each other type of member?

A.h's Code:

#ifndef ah#define Ahclass b;class A {b b;}; #endif

B.h's Code:

#ifndef bh#define bh#include "A.h" class B {    a A;}; #endif

There is a problem with this code, because if you statically define the object, a, a, the definition of one object must not be visible to the definition of the other object, so the definition fails. If all are used to include each other, depending on the compiler's order, a definition must not be visible. However, the predecessor declaration cannot define the object.

Solution:

The solution to this situation is to use a reference in the class defined by the predecessor declaration as a pointer, and the definition of the pointer does not need to be visible for that class.

Additional questions:

A.h

#ifndef ah#define ahclass b;class A {b* b;void Setb () {B->haha ();} ~a () {delete b;}}; #endif

D..

#ifndef bh#define bh#include "A.h" class B {    A a;void haha () {}}; #endif

However, a class that uses a predecessor declaration to cause a pointer member to be defined appears at the beginning of the warning warning because the definition is not visible.

"Warning C4150: Delete pointer to incomplete" B "type; no destructor called"

Another problem is that you cannot use this pointer to call members of this class in the. h file, because the definition is not visible.

"Error C2227: The left side of"->haha "must point to class/struct/union/generic type"

Solution:

At this point, you need to redefine a. cpp file for all member function implementations of A.h, and then the. cpp file to include the header file declaration of the pointer member class, where the definition is visible to define the destructor and invoke the class member of the pointer.

A.h

#ifndef ah#define ahclass b;class A {public:b* b;void setb (); ~a ();}; #endif

  

D..

#ifndef bh#define bh#include "A.h" class B {public:    A a;void haha () {}}; #endif

  

A.cpp

#include "A.h" #include "B.h" A::~a () {delete B;} void A::setb () {B->haha ();}

  

The problem is solved.

Problems with two classes referencing each other in C + +

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.