<small id='3k0YH'></small><noframes id='3k0YH'>

    <tfoot id='3k0YH'></tfoot>
  1. <i id='3k0YH'><tr id='3k0YH'><dt id='3k0YH'><q id='3k0YH'><span id='3k0YH'><b id='3k0YH'><form id='3k0YH'><ins id='3k0YH'></ins><ul id='3k0YH'></ul><sub id='3k0YH'></sub></form><legend id='3k0YH'></legend><bdo id='3k0YH'><pre id='3k0YH'><center id='3k0YH'></center></pre></bdo></b><th id='3k0YH'></th></span></q></dt></tr></i><div id='3k0YH'><tfoot id='3k0YH'></tfoot><dl id='3k0YH'><fieldset id='3k0YH'></fieldset></dl></div>
      <bdo id='3k0YH'></bdo><ul id='3k0YH'></ul>

  2. <legend id='3k0YH'><style id='3k0YH'><dir id='3k0YH'><q id='3k0YH'></q></dir></style></legend>

      声明 C++ 不可变类的惯用方法

      Idiomatic Way to declare C++ Immutable Classes(声明 C++ 不可变类的惯用方法)
    1. <tfoot id='XaFT7'></tfoot>
        1. <legend id='XaFT7'><style id='XaFT7'><dir id='XaFT7'><q id='XaFT7'></q></dir></style></legend>

          <small id='XaFT7'></small><noframes id='XaFT7'>

            <bdo id='XaFT7'></bdo><ul id='XaFT7'></ul>

              <i id='XaFT7'><tr id='XaFT7'><dt id='XaFT7'><q id='XaFT7'><span id='XaFT7'><b id='XaFT7'><form id='XaFT7'><ins id='XaFT7'></ins><ul id='XaFT7'></ul><sub id='XaFT7'></sub></form><legend id='XaFT7'></legend><bdo id='XaFT7'><pre id='XaFT7'><center id='XaFT7'></center></pre></bdo></b><th id='XaFT7'></th></span></q></dt></tr></i><div id='XaFT7'><tfoot id='XaFT7'></tfoot><dl id='XaFT7'><fieldset id='XaFT7'></fieldset></dl></div>

                <tbody id='XaFT7'></tbody>
                本文介绍了声明 C++ 不可变类的惯用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                所以我有一些非常广泛的功能代码,其中主要数据类型是不可变的结构/类.通过将成员变量和任何方法设为 const,我一直在声明不变性的方式是实际上不可变的".

                So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const.

                struct RockSolid {
                   const float x;
                   const float y;
                   float MakeHarderConcrete() const { return x + y; }
                }
                

                这真的是 C++ 中我们应该这样做"的方式吗?或者有更好的方法吗?

                Is this actually the way "we should do it" in C++? Or is there a better way?

                推荐答案

                你提出的方法完全没问题,除非在你的代码中你需要对 RockSolid 变量进行赋值,就像这样:

                The way you proposed is perfectly fine, except if in your code you need to make assignment of RockSolid variables, like this:

                RockSolid a(0,1);
                RockSolid b(0,1);
                a = b;
                

                这将不起作用,因为编译器会删除复制赋值运算符.

                This would not work as the copy assignment operator would have been deleted by the compiler.

                因此,另一种方法是将结构重写为具有私有数据成员且仅具有公共常量函数的类.

                So an alternative is to rewrite your struct as a class with private data members, and only public const functions.

                class RockSolid {
                  private:
                    float x;
                    float y;
                
                  public:
                    RockSolid(float _x, float _y) : x(_x), y(_y) {
                    }
                    float MakeHarderConcrete() const { return x + y; }
                    float getX() const { return x; }
                    float getY() const { return y; }
                 }
                

                这样,您的 RockSolid 对象是(伪)不可变的,但您仍然可以进行赋值.

                In this way, your RockSolid objects are (pseudo-)immutables, but you are still able to make assignments.

                这篇关于声明 C++ 不可变类的惯用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                <tfoot id='lLjLU'></tfoot>
              1. <small id='lLjLU'></small><noframes id='lLjLU'>

                  <tbody id='lLjLU'></tbody>
                    <legend id='lLjLU'><style id='lLjLU'><dir id='lLjLU'><q id='lLjLU'></q></dir></style></legend>

                        • <bdo id='lLjLU'></bdo><ul id='lLjLU'></ul>
                          <i id='lLjLU'><tr id='lLjLU'><dt id='lLjLU'><q id='lLjLU'><span id='lLjLU'><b id='lLjLU'><form id='lLjLU'><ins id='lLjLU'></ins><ul id='lLjLU'></ul><sub id='lLjLU'></sub></form><legend id='lLjLU'></legend><bdo id='lLjLU'><pre id='lLjLU'><center id='lLjLU'></center></pre></bdo></b><th id='lLjLU'></th></span></q></dt></tr></i><div id='lLjLU'><tfoot id='lLjLU'></tfoot><dl id='lLjLU'><fieldset id='lLjLU'></fieldset></dl></div>