C++中的二义性问题

直接贴代码吧。。。没啥好说的。。。

 

#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct Base1
{
    void print(int) const{
        cout<<"Base1"<<endl;
    }
protected:
    int ival;
    double dval;
    char cval;
private:
    int *id;
};
struct Base2
{
    void print(double )const{
        cout<<"Base2"<<endl;
    }
protected:
    double fval;
private:
    double dval;
};
struct Derived:public Base1
{
    void print(std::string) const{
        cout<<"Derived"<<endl;
    }
protected:
    std::string sval;
    double dval;
};
struct MI:public Derived,public Base2
{
    void print(std::vector<double>)
    {
        cout<<"MI"<<endl;
    }
    void f()
    {
        dval=3.1;//导致二义性,就是因为分不清到底是Derived里头的dval还是Base1的dval
    }
protected:
    int *ival;
    std::vector<double>dvec;
};

int main(int argc, char *argv[])
{
    MI mi;
    mi.print(42);//同样是二义性,得写成mi.Base1::print(42)
    return 0;
}

 

转载于:https://www.cnblogs.com/ccXgc/p/9023298.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值