c++通过模板化基类重写祖父类方法是一个好的设计吗?

发布于 2025-01-11 13:53:59 字数 1441 浏览 0 评论 0原文

要求:第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文