在两个类中共享相同的函数调用,并在不需要时避免空实例化
Share same function call in two classes and avoid empty instantiation when not necessary
我的问题:是否可以在两个继承的类(比如Base
和Derived
)之间共享对函数的相同调用,并且---关键点---当该函数严格特定于Base
时,避免在Derived
中使用空实例化,或者当该函数严格特定于Derived
时,Base
中避免空实例化?
共享同一调用的原因是几乎所有代码都是通用的,只是需要单独解决一些特殊性。例如:
class Base
{
virtual BaseSpecific(); // Useful in Base
virtual DerivedSpecific(); // Useless in Base
};
class Derived : Base
{
BaseSpecific() override; // Useless in Derived
DerivedSpecific() override; // Useful in Derived
};
// f() is so similar between Base and Derived that is shared common in Base
Base::f()
{
...
BaseSpecific();
DerivedSpecific();
...
}
Derived::BaseSpecific() {} // empty implementation in Derived, avoidable?
Base::BaseSpecific() { ... } // define the specific behavior in Base, OK
// or
Derived::DerivedSpecific() { ... } // define the specific behavior in Derived, OK
Base::DerivedSpecific() {} // empty implementation in Base, avoidable?
C++是否为此提供了解决方案?这是不可避免的吗?
提前谢谢你。
PD:这类似于这个线程,但在实现上有所不同。
final
关键字禁止覆盖该函数。所以BaseSpecific
适合使用。如果我们不希望DerivedSpecific
在Base
中实现,我们应该将其声明为pure abstract virtual function
.
class Base
{
virtual void BaseSpecific() final; // Useful in Base
virtual void DerivedSpecific() = 0; // Not possible to implement here with this declaration
};
class Derived : Base
{
// void BaseSpecific() override; // Error, not possible to override
void DerivedSpecific() override; // It is possible to override
};
但如果你这么说
因为 Base 和 Derived 之间的代码 90% 相同,并且对于 剩下的10%,是专门做的。有可能改进吗?
合适的解决方案可能是实现模板方法模式。
借助链接示例的可能实现可能是:
#include <iostream>
#include <memory>
class Base
{
public:
void templateMethod()
{
stepOne();
stepTwo();
stepThree();
}
protected:
void stepOne() // And i dont want it to be overriden
{ // so it is not virtual
std::cout << "%45 of the code done." << std::endl;
}
virtual void stepTwo() = 0;
void stepThree() // Again i dont want it to be overridden
{ // so it is not virtual
std::cout << "Remaining %45 of the work done." << std::endl;
}
};
class Derived : public Base
{
protected:
void stepTwo() override final // I want it to be final
{
std::cout << "%10 of the work done." << std::endl;
}
};
int main()
{
auto obj = std::make_unique<Derived>();
obj->templateMethod();
}
输出:
完成代码的 %45。
%10 已完成的工作。
剩余 %45 已完成的工作。
在线运行
还可以通过更改方法的声明来对模式执行条件操作。
例如:
bool stepTwo(); // Declaration
...
...
if ( stepTwo() ) // If second step succeeded
stepThree(); // Perform step three
相关文章:
- 为什么转换函数声明不需要至少一个定义类型说明符
- C++,QT. QShortcut的函数调用不存在
- 为什么使用指向函数的指针调用虚函数时不需要指针"this"?
- C++函数调用不起作用
- 循环迭代后,C 函数调用不会再次调用
- 结构构造函数调用不明确
- 可以't更改船舶位置,函数调用不起作用
- 函数调用不起作用,矢量访问数据
- 功能参数与函数调用不匹配
- C++ 与函数调用不匹配
- 函数调用不起作用
- 析构函数体内的函数调用不起作用
- 为什么这些重载函数调用不明确
- 对象将不带参数的函数调用到需要参数的函数
- 函数调用是否需要表达式
- boost库更新后,模板函数调用不起作用
- 函数调用不显示 cout
- 静态函数调用不起作用 (C++)
- 为什么在调用 localtime() 函数之前不需要 malloc 结构 tm 指针?
- 在c++ 11中,是否有一种方法可以在调用以任何可调用对象(包括绑定方法)作为参数的函数时不需要模板参数