Abstract#
It can be said that regular inheritance constructs the base class first and then constructs the derived class, while virtual inheritance embeds the base class into the derived class and constructs it only once to ensure that the virtual base class is only constructed once.
When the constructor of the base class contains the output statement "constructor", there will be different output results in the case of virtual inheritance and regular inheritance.
Consider the following example code:
#include <iostream>
class Base {
public:
Base() {
std::cout << "constructor" << std::endl;
}
};
class Derived : public virtual Base {
public:
Derived() : Base() {
}
};
int main() {
Derived d;
return 0;
}
In the above code, the Derived
class inherits the Base
class through virtual inheritance and calls the constructor of the base class in the constructor. When we run this code, we will find that "constructor" is only output once. This is because virtual inheritance ensures that the virtual base class (in this case, the Base
class) is only constructed once, even in multiple levels of inheritance. Therefore, the constructor of the base class Base
is only called once in the constructor of the derived class Derived
.
Now consider the case of regular inheritance:
#include <iostream>
class Base {
public:
Base() {
std::cout << "constructor" << std::endl;
}
};
class Derived : public Base {
public:
Derived() : Base() {
}
};
int main() {
Derived d;
return 0;
}
In this example, the Derived
class inherits the Base
class through regular inheritance and calls the constructor of the base class in the constructor. When running this code, "constructor" will be output twice, once from the constructor call of the base class and once from the constructor call of the derived class.
Therefore, virtual inheritance ensures that the virtual base class is only constructed once, while regular inheritance calls the constructor of the base class in each inheritance level. This is an important difference in constructor calls between virtual inheritance and regular inheritance.
In the case of virtual inheritance, one "constructor" output comes from the constructor call of the final derived class.
Consider the following example code:
#include <iostream>
class Base {
public:
Base() {
std::cout << "constructor" << std::endl;
}
};
class Intermediate : public virtual Base {
public:
Intermediate() : Base() {
}
};
class Derived : public Intermediate {
public:
Derived() : Intermediate() {
}
};
int main() {
Derived d;
return 0;
}
In this example, there is an intermediate class Intermediate
that inherits the Base
class through virtual inheritance. The derived class Derived
indirectly inherits the Base
class by inheriting the Intermediate
class through regular inheritance.
When we run this code, "constructor" will only be output once. This output comes from the constructor call of the final derived class Derived
, which calls the constructor of the Intermediate
class, and the constructor of the Intermediate
class calls the constructor of the base class Base
.
Therefore, in virtual inheritance, one "constructor" output comes from the constructor call of the final derived class, which calls the constructor of the virtual base class. This ensures that the virtual base class is only constructed once.
In regular inheritance, when constructing a derived class object, the base class part is constructed first, and then the derived class part is constructed on top of it. This means that the constructor of the base class is called before the constructor of the derived class. Therefore, in regular inheritance, the base class is constructed first, and then the derived class.
In virtual inheritance, the constructor of the base class is only called once, regardless of the depth of the inheritance hierarchy. Virtual inheritance introduces additional pointers or mechanisms to embed the virtual base class into the derived class to ensure that the virtual base class is only constructed once. In the constructor call chain of the derived class, the final derived class is responsible for calling the constructor of the virtual base class, instead of each derived class calling the constructor of the base class.
So, it can be said that regular inheritance constructs the base class first and then constructs the derived class, while virtual inheritance embeds the base class into the derived class and constructs it only once to ensure that the virtual base class is only constructed once.
This difference is to solve the problems in multiple inheritance, such as the diamond inheritance problem. The introduction of virtual inheritance allows for more flexible management and initialization of the base class part's members in the case of multiple inheritance.
So, does regular construction mean constructing a base class first and then constructing a derived class? Does virtual inheritance mean embedding the base class into the derived class and constructing it only once?
In regular inheritance, when constructing a derived class object, the base class part is constructed first, and then the derived class part is constructed on top of it. This means that the constructor of the base class is called before the constructor of the derived class. Therefore, in regular inheritance, the base class is constructed first, and then the derived class.
In virtual inheritance, the constructor of the base class is only called once, regardless of the depth of the inheritance hierarchy. Virtual inheritance introduces additional pointers or mechanisms to embed the virtual base class into the derived class to ensure that the virtual base class is only constructed once. In the constructor call chain of the derived class, the final derived class is responsible for calling the constructor of the virtual base class, instead of each derived class calling the constructor of the base class.
So, it can be said that regular inheritance constructs the base class first and then constructs the derived class, while virtual inheritance embeds the base class into the derived class and constructs it only once to ensure that the virtual base class is only constructed once.
This difference is to solve the problems in multiple inheritance, such as the diamond inheritance problem. The introduction of virtual inheritance allows for more flexible management and initialization of the base class part's members.