且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不使用public或private的情况下继承其他类

更新时间:2022-11-03 21:17:26

这里根本没有继承.
您只是在创建class A的对象作为class B的成员变量.
这与在类内部或main函数中的class B对象中创建std::string对象的方式相同.
因此,例如,您将无法执行任何多态性.
A* ptr = new B;将引发错误,因为这需要继承.
There is no inheritance here at all.
You''re simply creating an object of class A as a member variable of class B.
This is the same as how you created a std::string object inside the class or an object of class B in the main function.
So, for example, you will not be able to perform any polymorphism.
A* ptr = new B; will throw an error because this needs inheritance.