virtual public
时间: 2023-12-08 22:05:10 浏览: 55
"Virtual public" refers to a group of people who interact with each other through online platforms, such as social media, forums, and chat rooms. They may share common interests, goals, or experiences, and use the internet to communicate and collaborate with each other. Virtual publics can be geographically dispersed and may not necessarily have any physical contact with each other. They are often formed around specific topics or events and can have a significant impact on public opinion and discourse.
相关问题
virtual public 和public 在类继承里的区别
在类继承中,使用public关键字表示派生类可以直接访问基类的公有成员。这意味着在派生类中可以直接使用基类的公有成员函数和公有变量。
使用virtual public关键字表示虚拟继承和公有继承的组合。虚拟继承用于解决多重继承中的菱形继承问题。当一个派生类通过公有继承从多个基类派生时,如果这些基类之间存在相同的基类,那么每个共同基类的对象就会在派生类中有多个副本。这可能导致二义性和资源浪费。通过使用虚拟继承,可以确保共同基类的对象只有一个副本,并避免二义性。
总结起来,使用public关键字的继承表示派生类可以直接访问基类的公有成员,而使用virtual public关键字的继承表示虚拟继承和公有继承的组合,用于解决多重继承中的菱形继承问题。
#include <iostream> using namespace std; class A { int a; public: A(){ cout<<"Constructing A"<<endl; }}; class B { public: B(){ cout<<"Constructing B"<<endl;}}; class C { public: C(){ cout<<"Constructing C"<<endl;}}; class B1:virtual public B, virtual public A{ public: B1(int i){ cout<<"Constructing B1"<<endl; }}; class B2:public A, virtual public B { public: B2(int j){ cout<<"Constructing B2"<<endl; }}; class D: public B2, public B1, virtual public C { public: D(int m,int n): B1(m),B2(n){ cout<<"Constructing D"<<endl; } A a; }; int main(){ D d(1,2); }输出过程
输出过程如下:
Constructing A
Constructing B
Constructing B1
Constructing A
Constructing B
Constructing B2
Constructing C
Constructing D
首先创建了对象d,接着创建了B1和B2对象,由于B1和B2都继承了虚拟的B基类和A基类,所以先创建了A和B对象,然后创建了B1和B2对象。接着创建了C对象,最后创建了D对象。输出的顺序即为对象创建的顺序。
阅读全文