c++通过模板化基类重写祖父类方法是一个好的设计吗?
要求:第 2 层需要更改祖父类(即 Base)的一些功能。此更改的功能对于 Dervied_Layer_2_Case_A 和 Dervied_Layer_2_Case_B 是相同的。
您认为使用以下类型的解决方案有任何负面影响吗?我问这个是因为我以前没有见过这样的模式。
struct Base
{
virtual void doSomething() { ... }
};
///// Layer 1 ////////////////////
template <class BaseType = Base>
struct Dervied_Layer_1_Case_A : public BaseType
{
static_assert(std::is_base_of<Base, BaseType>::value);
//This class uses other functionality from Base. Hence the derivation
void runA() {calls BaseType::doSomething() while working}
};
template <class BaseType = Base>
struct Dervied_Layer_1_Case_B : public BaseType
{
static_assert(std::is_base_of<Base, BaseType>::value);
//This class uses other functionality from Base. Hence the derivation
void runB() {calls BaseType::doSomething() while working}
};
////////Layer 2 //////////////
struct BaseLayer2 : public Base
{
//Only below behavior need to be changed. Other functionality of Base used without change
void doSomething() override { ...differnet implementation.. }
};
struct Dervied_Layer_2_Case_A : public Dervied_Layer_1_Case_A <BaseLayer2>
{
//some of the functionality of DerviedCase_1_A is changed
};
struct Dervied_Layer_2_Case_B : public Dervied_Layer_1_Case_B <BaseLayer2>
{
//some of the functionality of DerviedCase_1_B is changed
}
Requirement : Some of the functionality of grand parent class (i.e. Base) need to be changed for layer 2. This changed functionality is same for both Dervied_Layer_2_Case_A and Dervied_Layer_2_Case_B.
Do you see any negative impact of using below type of solution ? I am asking this becoz I haven't seen such pattern before.
struct Base
{
virtual void doSomething() { ... }
};
///// Layer 1 ////////////////////
template <class BaseType = Base>
struct Dervied_Layer_1_Case_A : public BaseType
{
static_assert(std::is_base_of<Base, BaseType>::value);
//This class uses other functionality from Base. Hence the derivation
void runA() {calls BaseType::doSomething() while working}
};
template <class BaseType = Base>
struct Dervied_Layer_1_Case_B : public BaseType
{
static_assert(std::is_base_of<Base, BaseType>::value);
//This class uses other functionality from Base. Hence the derivation
void runB() {calls BaseType::doSomething() while working}
};
////////Layer 2 //////////////
struct BaseLayer2 : public Base
{
//Only below behavior need to be changed. Other functionality of Base used without change
void doSomething() override { ...differnet implementation.. }
};
struct Dervied_Layer_2_Case_A : public Dervied_Layer_1_Case_A <BaseLayer2>
{
//some of the functionality of DerviedCase_1_A is changed
};
struct Dervied_Layer_2_Case_B : public Dervied_Layer_1_Case_B <BaseLayer2>
{
//some of the functionality of DerviedCase_1_B is changed
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论