我对虚拟继承有疑问。我很感兴趣为什么这段代码会打印 1000(来自“二”类)而不是 3(来自“一”类)
这是代码:
#include <iostream>
using namespace std;
class A {
protected:
int number;
public:
A (int a=0) {number=a;}
};
class one:virtual public A {
public:
one (int a=3) {number=a;}
void print() {cout<<number<<endl;}
};
class two :virtual public A {
public:
two (int a=1000) {number =a;}
void print() { cout<<number<<endl; }
};
class B:public one,public two {
public:
void print() { cout<<number<<endl; }
};
int main () {
B A;
A.print();
}