在C++中是否可以使用另一个基类在派生类中提供接口(即抽象基类)的实现?
class Base
{
virtual void myfunction() {/*...*/};
}
class Interface
{
virtual void myfunction() = 0;
}
class Derived
: public Base, public Interface
{
// myfunction is implemented by base
}
Run Code Online (Sandbox Code Playgroud)
上面应该编译,但实际上不起作用.有没有办法达到这个效果?
如果有人关心,想要这个的原因是(对我的应用程序)使用来自另一个项目/命名空间的通用库来提供我的项目中的接口的实现是有意义的.我可以把所有东西都包起来,但这似乎是很多额外的开销.
谢谢.
Adr*_*thy 12
如果Base不是从中派生出来的Interface,那么你必须要有转发电话Derived.在你必须编写额外代码的意义上,它只是"开销".我怀疑优化器会使它像你原来的想法一样有效.
class Interface {
public:
virtual void myfunction() = 0;
};
class Base {
public:
virtual void myfunction() {/*...*/}
};
class Derived : public Interface, public Base {
public:
void myfunction() { Base::myfunction(); } // forwarding call
};
int main() {
Derived d;
d.myfunction();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
class Interface
{
virtual void myfunction() = 0;
}
class Base : public Interface
{
virtual void myfunction() {/*...*/};
}
class Derived
: public Base
{
// myfunction is implemented by base
}
Run Code Online (Sandbox Code Playgroud)
没有.(不管怎么说)
您可能会误解其他语言,如Java,C#,ActionScript等.
在C++中,多重继承和虚拟类的管理方式使接口(在其他语言中使用)过时.在其他语言中,接口用于修复由于缺少多重继承而导致的问题(好的或坏的,它是一种选择).
因此,如果您想要做的只是提供一些提供默认实现的虚拟方法的通用接口,那么只需在基类中实现:
class Interface
{
virtual void myfunction() { /*...*/ } ; //default implementation
virtual void yourFunction() = 0 ; // this one HAVE TO be implemented by the user
}
class Derived
: public public Interface // dont' need another clas
{
// myfunction is implemented by base
void yourFunction(); // have to implement yourFunction
}
class DerivedB
: public public Interface // dont' need another clas
{
void myFunction();// myfunction is implemented by base but we implement it for this specific class
void yourFunction(); // have to implement yourFunction
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您想提供几个具有相同接口的基类,那么请认为您的接口类是其他类的基础
// in this order
class Interface
{
virtual void myfunction() = 0;
}
class BaseA : public Interface
{
// here "virtual" is optional as if the parent is virtual, the child is virtual too
virtual void myfunction() {/*...*/}; // BaseA specific implementation
}
class BaseB : public Interface
{
virtual void myfunction() {/*...*/}; // BaseB specific implementation
}
Run Code Online (Sandbox Code Playgroud)
然而,有一种不太容易阅读(读取:不推荐)的方式来提供默认实现但是迫使用户明确地说出他是否想要使用它.它利用了这样一个事实,即使是纯虚函数也可以拥有可以调用的默认实现:
class Interface
{
virtual void myfunction() { /*...*/ } ; //default implementation
virtual void yourFunction() = 0 ; // this one HAVE TO be implemented by the user BUT provide a default implementation!
}
// in Interface.cpp
void Interface::yourFunction() // default implementation of the virtual pure function
{ /*...*/ }
// in Derived.h
class DerivedA
: public public Interface // dont' need another clas
{
// myfunction is implemented by base
void yourFunction(); // have to implement yourFunction -- DerivedA specific
}
class DerivedB
: public public Interface // dont' need another clas
{
void myFunction();// myfunction is implemented by base but we implement it for this specific class
void yourFunction() { Interface::yourFunction(); } // uses default implementation of yourFunction, hidden but existing
}
Run Code Online (Sandbox Code Playgroud)
但是不要这样做.
| 归档时间: |
|
| 查看次数: |
11141 次 |
| 最近记录: |