多重继承(multiple inheritance)歧义(ambiguous)
本文地址: http://blog.csdn.net/caroline_wendy/article/details/23737553
多重继承, 如果基类(base class)的成员函数名称相同,重载(overload)函数调用, 会忽略public和private, 选取最佳的匹配函数,
匹配度相同, 则会造成歧义, 产出错误:request for member ‘xxx‘ is ambiguous, (GCC)
如需使用, 可以添加具体的域操作符, 指定类("class::method()").
代码如下:
/* * test.cpp * * Created on: 2014.04.14 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <string> using namespace std; class BorrowableItem { public: //共有 void checkout() { std::cout << "BorrowableItem" << std::endl; } }; class ElectronicGadget { private: //私有 bool checkout() const { return true; }; }; class MP3Player: public BorrowableItem, public ElectronicGadget {}; int main() { MP3Player mp; mp.BorrowableItem::checkout(); //不添加域操作符会产生歧义 return 0; }
输出:
BorrowableItem
C++ - 多重继承(multiple inheritance)歧义(ambiguous),布布扣,bubuko.com