You must create a base class that will have 4 heirs. The problem is that I need a base class in one instance, since there will be stored pointers to inherited objects + there are inherited classes that will not be created immediately and I need to determine which ones will be created and which ones will not be in the array.

The question is: There is a code:

header class dataBase { public: std::vector<dataBase*> data; dataBase(); int a=0; static int sl; }; class type1 : public virtual dataBase { public: type1(); int b; }; class type2: public virtual dataBase { public: type2(); int c; }; 

where in the implementation for each:

 int dataBase::sl=0; dataBase::dataBase() { sl++; a=sl; } type1::type1(){ data.insert(data.end(), this); b=a; } type2::type2() { data.insert(data.end(), this); c=a; } 

The problem is that by defining a class heir to a virtual one, as I understand it, it makes it possible not to create a copy of the base class, it still creates a copy. If you look at the counter of a static variable, it will indicate that the base class was created two times.

And if you write to variable a from the inherited class type1, then in type2 you get another number (random, if you discard the constructor code.)

Please help me figure it out. Thanks in advance.

  • Write what exactly you want to get, I suspect a mistake in architecture (perhaps you need not inheritance but composition for example). Or just use a static field as an option. But without an understanding of the problem, I will not prompt it at once. - pavel
  • Good day. I would like to be able to store the data structure (there are different variables in each class) in memory, and I would also like to have data management from one class. (for example: write class fields to a text file) Thanks for earlier. - Pasha
  • one
    In order to get the effect of not creating a copy of the base class from virtual inheritance, you need: 1) at least 3 levels of the class hierarchy, 2) multiple inheritance. - AnT
  • thanks for the information. - Pasha

1 answer 1

as I understood it gives the opportunity not to create a copy of the base class, it still creates a copy

He guarantees that among all the ancestors of this class, any ancestor will be created only one in the hierarchy for this class.
In your case, each type1 and type2 has ONE unique ancestor, each class has unique ancestors, respectively, each ancestor will be created for each class of descendant. Therefore, your virtual here plays no role.
virtual can only matter in case of multiple inheritance, when a class has several ancestors, and we want to have no ancestor copies in the inheritance hierarchy.

Example:

 class A { }; class B: public A {}; class C: public A {}; class D: public B, public C {}; 

The class D hierarchy has the following descendants: B, A, C, A
By doing like this

 class A { }; class B: public virtual A {}; class C: public virtual A {}; class D: public B, public C {}; 

we say that we in the class D hierarchy need only have one descendant of A, respectively, instead of creating 2 instances of class A, ONLY ONE ONLY will be created: A, B, C

There is an assumption that you need static here for your base class field, which will guarantee you that it will be common to all class instances.

  • Thanks for the answer. Now everything became clear. - Pasha