有两个基类具有相同的函数名.我想继承它们,并以不同的方式搭乘每种方法.如何使用单独的声明和定义(而不是在类定义中定义)?
#include <cstdio>
class Interface1{
public:
virtual void Name() = 0;
};
class Interface2
{
public:
virtual void Name() = 0;
};
class RealClass: public Interface1, public Interface2
{
public:
virtual void Interface1::Name()
{
printf("Interface1 OK?\n");
}
virtual void Interface2::Name()
{
printf("Interface2 OK?\n");
}
};
int main()
{
Interface1 *p = new RealClass();
p->Name();
Interface2 *q = reinterpret_cast<RealClass*>(p);
q->Name();
}
Run Code Online (Sandbox Code Playgroud)
我没能在VC8中移出定义.我发现Microsoft特定关键字__interface可以成功完成这项工作,代码如下:
#include <cstdio>
__interface Interface1{
virtual void Name() = 0;
};
__interface Interface2
{
virtual void Name() = 0;
}; …Run Code Online (Sandbox Code Playgroud) 我有一个与C++中的多继承相关的基本问题.如果我有如下所示的代码:
struct base1 {
void start() { cout << "Inside base1"; }
};
struct base2 {
void start() { cout << "Inside base2"; }
};
struct derived : base1, base2 { };
int main() {
derived a;
a.start();
}
Run Code Online (Sandbox Code Playgroud)
这给出了以下编译错误:
1>c:\mytest.cpp(41): error C2385: ambiguous access of 'start'
1> could be the 'start' in base 'base1'
1> or could be the 'start' in base 'base2'
Run Code Online (Sandbox Code Playgroud)
有没有办法能够start()使用派生类对象从特定基类调用函数?
我现在不知道用例但是..仍然!
标题可能令人困惑.
假设我们有以下设置;
class A
{
public:
virtual void fn() = 0;
};
class B
{
public:
virtual int fn() {};
};
class C: public A, public B
{
};
Run Code Online (Sandbox Code Playgroud)
有什么办法来定义A::fn的class C?