多继承可能引发父类中有同名成员的出现,需要加作用域区分
#include
#include
using namespace std;
class Base
{
public:
Base() {
m_A = 100;
}
int m_A;
};
class Base2 {
public:
Base2()
{
m_A = 200;
}
int m_A;
};
class Son: public Base,public Base2
{
public:
Son()
{
m_C = 300;
m_D = 400;
}
int m_C ;
int m_D ;
};
void test01()
{
Son s;
cout << "sizeof Son = " << sizeof(s) << endl;
cout << s.Base::m_A<< endl;
cout << s.Base2::m_A << endl;
cout << s.m_C << endl;
}
int main()
{
test01();
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51