如何初始化共享复杂初始化代码的多个常量成员变量? [英] How to initialize multiple constant member variables that shares complex initialization code?

查看:116
本文介绍了如何初始化共享复杂初始化代码的多个常量成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们介绍这个简单的例子:

Let's introduce this simple example:

#include <cmath>

class X
{
public: // Members

    /// A ^ B + A
    int A;

    /// A ^ B + B
    int B;

public: // Specials

    X(
        const int & A,
        const int & B
    )
        : A(A)
        , B(B)
    {
        const auto Pow = static_cast<int>(std::pow(A, B));

        this->A += Pow;
        this->B += Pow;
    }
};

琐事

  • 引入的类具有两个成员变量:AB.
  • 它们的值分别为A ^ B + AA ^ B + B.
  • 它们两者共享通用的复杂初始化代码(假设std::pow是复杂的).
  • Trivia

    • Introduced class has two member variables: A and B.
    • They take value of A ^ B + A and A ^ B + B, respectively.
    • Both of them shares common complex initialization code (let's assume std::pow is complex).
    • 我想同时成为AB成员const.

      如何做到这一点而无需重复复杂的初始化( ie 避免两次调用std::pow)?

      How to do that without repeating complex initialization (ie avoid calling std::pow twice)?

      #include <cmath>
      
      class X
      {
      public: // Members
      
          /// A ^ B + A
          const int A;
      
          /// A ^ B + B
          const int B;
      
      public: // Helpers
      
          struct Init
          {
          public: // Members
      
              int A;
              int B;
      
          public: // Specials
      
              Init(
                  const int & A,
                  const int & B
              )
                  : A(A)
                  , B(B)
              {
                  const auto Pow = static_cast<int>(std::pow(A, B));
      
                  this->A += Pow;
                  this->B += Pow;
              }
          };
      
      public: // Specials
      
          X(
              const Init& Init
          )
              : A(Init.A)
              , B(Init.B)
          {};
      
          X(
              const int & A,
              const int & B
          )
              : X(Init(
                  A,
                  B
              ))
          {};
      };
      

      1. 创建具有X类的过去版本角色的struct Init.
      2. 使X成员const,而使Init成员非const.
      3. 使用构造函数委托将构造函数参数重定向到Init.
      4. 将非const成员变量从Init移动到X,并将其设置为const.
      1. Create struct Init that takes role of past version of class X.
      2. Make X members const while keep Init members non const.
      3. Use constructor delegation to redirect constructor arguments to Init.
      4. Move non const member variables from Init to X and make them const.
        • Note there is no std::move as int is TriviallyCopyable.

      但是,我的解决方案似乎过于复杂.任何帮助将不胜感激.

      However, my solution seems overcomplicated. Any help would be appreciated.

      • 制作另一个X成员变量,该成员变量将存储通用代码结果( ie std::pow).
      • X类之外添加另一级别的间接寻址( eg X引入基类).
      • Make another X member variable that will store common code result (ie std::pow).
      • Add another level of indirection outside X class (eg introduce base class for X).

      解决方案可以使用比C ++ 11更新的C ++版本.

      Solutions can use newer versions of C++ than C++11.

      推荐答案

      使用 func1func2func3中的逻辑/计算可以根据需要简单或复杂.

      The logic/computation in func1, func2, and func3 can be as simple or as complex as you need.

      这篇关于如何初始化共享复杂初始化代码的多个常量成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆