多继承

同时继承多个基类

  • class A : public B1, public B2,….
  • 引发二义性问题
  • 想解决二义性问题,就需要通过作用域来进行区分
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Base1
{
public:
    Base1()
    {
        this->m_A = 10;
    }
    int m_A;
};
class Base2
{
public:
    Base2()
    {
        this->m_A = 20;
    }
    int m_A;
};
class Son :public Base1, public Base2   //多个父类用,隔开
{
public:
    int m_C;
    int m_D;
};
void test01()
{
    Son s;  
    cout << sizeof(s) << endl;  //16

    //cout << s.m_A << endl;  //二义性 多继承会带来一些二义性问题

    cout << s.Base1::m_A << endl;  //解决二义性的方法  显示指定调用哪个积累的版本
    cout << s.Base2::m_A << endl;

}

int main()
{
    system("Pause");
    return 0;
}

结果:

原文地址:https://www.cnblogs.com/yifengs/p/15176938.html