This question has already been answered:

There is a BaseClass :

 // BaseClass.h #pragma once #include "ChildClass.h" class BaseClass { public: BaseClass(); ~BaseClass(); ChildClass obj; }; // BaseClass.cpp #include "BaseClass.h" BaseClass::BaseClass() { } BaseClass::~BaseClass() { } 

There is a ChildClass :

 // ChildClass.h #pragma once #include "BaseClass.h" class ChildClass { public: ChildClass(BaseClass &obj); ~ChildClass(); BaseClass obj; }; // ChildClass.cpp #include "ChildClass.h" ChildClass::ChildClass(BaseClass &obj) { this->obj = obj; } ChildClass::~ChildClass() { } 

Description: The reference to the BaseClass object is passed to the BaseClass and the internal object of the base class is initialized, which is located in the child class. In the base class, a field is declared, which must refer to the subsequently initialized object of the child class. Those. roughly speaking, it is necessary that the child class has access to its object through the base class.

The question is: did this architecture work quite well in .NET-е , what do not like the pluses and how can I get around the problem? In the pros - this example is not compiled, VS gives a variety of errors, error numbers: C2061 , C2146 , C4430 .

ps as far as I understood, given my poor knowledge of the pros, there exists some kind of recursive dependency of the header files and it is not quite clear why the preprocessor # pragma #pragma once does not work.

The pps solution in this reply is from comments: Include C ++ files. - a crutch (and with strong limitations), is there any normal solution?

Reported as a duplicate by Cerbo participants, aleksandr barakin , user194374, Denis , pavel 25 Aug '16 at 6:36 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Gentlemen, who like to put minuses to questions - the minuses cannot solve the problem. You at least argue in the comments of your actions. There is a fact - this code worked fine in .NET, how to port it to pluses? - Alexis
  • Please indicate what the problem is to get around. - Cerbo
  • @Cerbo, you need to implement the following behavior: the base class object is passed to the child classes through the constructors, the child class objects are initialized in the base class constructor. From any child class there is access to the object of another child class. The problem is precisely this, how to properly implement this? In my case, the example is not compiled, various errors occur. - Alexis
  • What you brought is not a problem, this is your goal. The problem is what prevents you from reaching your goal. Please describe the problem. - Cerbo
  • @Cerbo, the example is simply not compiled, the studio gives a variety of errors. If necessary, I will give the error numbers - Alexis

2 answers 2

Here, the development of the @Abyx idea in the code (sorry, the comment does not fit ...)

 // BaseClass.h #pragma once #include "ChildClass.h" class ChildClass; class BaseClass { public: BaseClass(ChildClass&obj); ~BaseClass(); ChildClass * obj; }; // BaseClass.cpp #include "BaseClass.h" BaseClass::BaseClass(ChildClass&obj) { this->obj = &obj; } BaseClass::~BaseClass() { } // ChildClass.h #pragma once #include "BaseClass.h" class BaseClass; class ChildClass { public: ChildClass(BaseClass &obj); ~ChildClass(); BaseClass * obj; }; // ChildClass.cpp #include "ChildClass.h" ChildClass::ChildClass(BaseClass &obj) { this->obj = &obj; } ChildClass::~ChildClass() { } 

Here is the second option - working through links:

 // BaseClass.h #pragma once #include "ChildClass.h" class ChildClass; class BaseClass { public: BaseClass(ChildClass&obj); ~BaseClass(); ChildClass& obj; }; // BaseClass.cpp #include "BaseClass.h" BaseClass::BaseClass(ChildClass&obj):obj(obj) { } BaseClass::~BaseClass() { } // ChildClass.h #pragma once #include "BaseClass.h" class BaseClass; class ChildClass { public: ChildClass(BaseClass &obj); ~ChildClass(); BaseClass& obj; }; // ChildClass.cpp #include "ChildClass.h" ChildClass::ChildClass(BaseClass &obj):obj(obj) { } ChildClass::~ChildClass() { } 
  • I would like to clarify, and how exactly the objects of classes are declared, if they are dependent on each other, can you give an example? - Alexis
  • Well, for example, vpaste.net/fte34 - Harry

The ChildClass::obj member must be a link or pointer.
And ChildClass must be initialized in the BaseClass constructor.

  • Please give a minimal example. - Alexis