C++中结构体的疑惑
#include <iostream>using namespace std;
struct V {
V(int a=0) { cout<<"V"<<a; }
};
struct A : virtual V {
A(int a);
};
struct B : virtual V {
B(int a=8);
};
struct C : A, B {
C(int a);
};
struct D : C, B {
D(int);
};
A::A(int i) :V(i) { cout<<"A"<<i; }
B::B(int i) { cout<<"B"<<i; }
C::C(int i) :A(i),B(i),V(i) { cout<<"C"<<i; }
D::D(int i) :C(i) { cout<<"D"<<i; }
int main(){
V v(1); cout<<endl;
A a(2); cout<<endl;
B b(3); cout<<endl;
C c(4); cout<<endl;
D d(5); cout<<endl;
return 0;
}
测试通不过,错误提示:
C:\Documents and Settings\IBM\桌面\tt.cpp(15) : error C2584: 'D' : direct base 'B' is inaccessible; already a base of 'C'
C:\Documents and Settings\IBM\桌面\tt.cpp(9) : see declaration of 'B'
另外,结构体和类有什么区别啊?怎么看上去差不多呢。。用法上有什么差异?