use*_*802 6 c++ gcc visual-c++
可能重复:
继承共享方法名称的接口
我有两个基类I1和I2纯虚函数void R() = 0;.我想在派生类IImpl要继承I1和I2并有不同的实现方式为I1::R()和I2::R().
下面的代码在MS VS 2005和2010中编译和工作.我在禁用语言扩展并且在警告级别4上进行编译.没有警告也没有错误.
我在gcc 4.2中尝试了相同的代码.它不编译.GCC报告错误:
error: cannot define member function 'I1::R' within 'IImpl'
Run Code Online (Sandbox Code Playgroud)
我的问题是:
谢谢!
#include <stdio.h>
class I1
{
public:
virtual void R() = 0;
virtual ~I1(){}
};
class I2
{
public:
virtual void R() = 0;
virtual ~I2(){}
};
class IImpl: public I1, public I2
{
public:
virtual void I1::R()
{
printf("I1::R()\r\n");
}
virtual void I2::R()
{
printf("I2::R()\r\n");
}
};
int main(int argc, char* argv[])
{
IImpl impl;
I1 *p1 = &impl;
I2 *p2 = &impl;
p1->R();
p2->R();
return 0;
}
Run Code Online (Sandbox Code Playgroud)