重构一个类

发布于 2024-10-04 10:35:57 字数 454 浏览 2 评论 0 原文

我有两个几乎相同的类,实际上每个成员函数都是相同的,每个成员都是相同的,每个成员函数都做完全相同的事情。这些类之间的唯一区别是我可以定义其类型的变量的方式:

AllocFactorScientific<102,-2> scientific;
AllocFactorLinear<1.2> linear;  

以下是它们的标题:

template<double&& Factor>
struct AllocFactorLinear;

template<short Mantissa, short Exponent, short Base = 10>
struct AllocFactorScientific

我的问题是如何从这些类中重构这些函数,使我只拥有一组函数而不是两个函数相同函数的集合。

I have two almost identical classes, in fact every member function is identical, every member is identical, every member function does exactly the same thing. The only difference between those classes is the way I can define variable of their type:

AllocFactorScientific<102,-2> scientific;
AllocFactorLinear<1.2> linear;  

Here are headers for them:

template<double&& Factor>
struct AllocFactorLinear;

template<short Mantissa, short Exponent, short Base = 10>
struct AllocFactorScientific

My question is how can I refactor those functions out of those classes that would allow me to have just one set of functions and not two sets of identical functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请恋爱 2024-10-11 10:35:57

提取第三个类中的所有常见行为(为了清楚起见,我在答案中省略了模板参数):

class CommonImpl
{
public:
  void doSomething() {/* ... */ }
};

然后我看到两个选择(至少从我的角度来看,它们几乎是等效的):

  • < p>使AllocFactorLinearAllocFactorScientific从此类私有继承,并使用using将您希望公开的成员函数引入范围内 指令:

    类 AllocFactorLinear :CommonImpl
    {
    民众:
      使用 CommonImpl::doSomething;
    };
    
  • 聚合 AllocFactorLinearAllocFactorScientific 中的实现类,并将所有调用转发到私有实现:

    类 AllocFactorLinear
    {
    民众:
      无效 doSomething() { impl_.doSomething(); }
    私人的:
      CommonImpl impl_;
    };
    

我个人会选择第一个解决方案。

Extract all the common behavior in a third class (I'm omitting the template arguments in my answer for the sake of clarity) :

class CommonImpl
{
public:
  void doSomething() {/* ... */ }
};

I then see two choices (which are, from my point of view at least, pretty much equivalent) :

  • Make AllocFactorLinear and AllocFactorScientific inherit privately from this class, and bring the member functions you wish to expose in scope with a using directives :

    class AllocFactorLinear : CommonImpl
    {
    public:
      using CommonImpl::doSomething;
    };
    
  • Aggregate the implementation class in AllocFactorLinear and AllocFactorScientific and forward all the calls to the private implementation :

    class AllocFactorLinear
    {
    public:
      void doSomething() { impl_.doSomething(); }
    private:
      CommonImpl impl_;
    };
    

I would personally go for the first solution.

心碎无痕… 2024-10-11 10:35:57

也许尝试使用此函数创建一些基类,并使用该基类的继承模板来执行这两个类。

Maybe try with creating some base class with this functions and do this 2 classes with templates on inheritance of this base class.

小猫一只 2024-10-11 10:35:57

为什么为此使用模板类?
你不能使用一个带有两个不同构造函数的非模板类吗?

Why do you use a templated class for this?
Couldnt you use a untemplated class with 2 different constructors?

雨落□心尘 2024-10-11 10:35:57

我认为它会是这样的:

template <typename Type>
struct AllocFactor {...};

然后你可以有Type,例如:

template <double&& Factor>
struct LinearConfig
{
    static double value() { return Factor;}
};

并且:

template <short Mantissa, short Exponent, short Base = 10>
struct FactorScientificConfig
{
    static double value() 
    {
       return some_expression_to_get_factor;
    }
};

你可以使用AllocFactor创建AllocFactor > 以及与 FactorScientificConfig 相对应的内容。然后,您可以使用静态成员函数返回为这两个类计算的值,以便 AllocFactor 可以使用 T::value() 作为报告的值通过配置类。

I think it would be something like:

template <typename Type>
struct AllocFactor {...};

and then you can have Type, for example:

template <double&& Factor>
struct LinearConfig
{
    static double value() { return Factor;}
};

and:

template <short Mantissa, short Exponent, short Base = 10>
struct FactorScientificConfig
{
    static double value() 
    {
       return some_expression_to_get_factor;
    }
};

You can create the AllocFactor using AllocFactor<LinearConfig<1.2>> and the corresponding with the FactorScientificConfig. Then you can use the static member function to return the value calculated for both the classes, so that AllocFactor<T> can use T::value() as the value reported by the config class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文