简体   繁体   English

如何在C ++中声明不可变类(Java示例)

[英]how can I declare an immutable class in C++ (Java Example)

How can I do this in C++? 我怎么能用C ++做到这一点?

//java example
public class MyImmutable {
     private final int numToBeInitializedOnConstructionOrCompilationError;

     public MyImmutable(int n) {
         this.numToBeInitializedOnConstructionOrCompilationError = n;
     }
}

With Java if the final property is not initialized when declared or on the constructor the compiler will error out, this code works since the member variable is initialized in the constructor. 对于Java,如果最终属性在声明时未初始化,或者在构造函数上编译器将错误输出,则此代码有效,因为成员变量在构造函数中初始化。 How can I enforce immutability in a class declaration in C++? 如何在C ++中的类声明中强制实现不变性?

(defining only public getter methods is not an acceptable answer, I know 'const' must be part of the solution) (仅定义公共getter方法不是一个可接受的答案,我知道'const'必须是解决方案的一部分)

If I do this in C++: 如果我在C ++中这样做:

class MyImmutable 
{
  private:
     const int numToBeInitializedOnConstruction;

  public:
     MyImmutable(int n);
};

MyImmutable::MyImmutable(int n) 
{
  numToBeInitializedOnConstruction = n;
}

I get the following errors, basically telling me I cannot assign the const member, so I guess const is way more strict than java's 'final', is there another keyword or is there a way to initialize a const after it's been declared? 我得到以下错误,基本上告诉我我不能分配const成员,所以我猜const比java''final'更严格,是否有另一个关键字,或者有一种方法在声明后初始化const?

$ gcc const.cc 
const.cc:10:10: error: constructor for 'MyClass' must explicitly initialize the const member
      'numToBeInitializedOnConstruction'
MyClass::MyClass(int n) {
         ^
const.cc:3:13: note: declared here
  const int numToBeInitializedOnConstruction;
            ^
const.cc:11:36: error: read-only variable is not assignable
  numToBeInitializedOnConstruction = n;
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
2 errors generated.

so how do I declare the equivalent of a java final member variable in C++? 那么如何在C ++中声明java final成员变量的等价物呢? is this possible? 这可能吗?

C++ can handle object immutability in a much nicer fashion than in Java. C ++可以以比Java更好的方式处理对象不变性。 First of all, you can make an immutable object out of nearly any type with the const qualifier. 首先,您可以使用const限定符从几乎任何类型中创建一个不可变对象。

const string str = "asd";
const MyClass myObj(1);

You can also have constant attributes like that numToBeInitializedOnConstruction you have, but it must be initialized in the constructor's member initialization list, like this: 你也可以使用像numToBeInitializedOnConstruction那样的常量属性,但它必须在构造函数的成员初始化列表中初始化,如下所示:

MyImmutable::MyImmutable(int n)
 : numToBeInitializedOnConstruction(n) // like this
{
}

Use initialization list like: 使用初始化列表如:

MyImmutable::MyImmutable(int n): numToBeInitializedOnConstruction(n) {}

EDIT: Just a better indent. 编辑:只是一个更好的缩进。

To declare an immutable object , simply declare it as const : 要声明一个不可变对象 ,只需将其声明为const

const Foo myImmutableFoo;

To declare immutable attributes of a class, you can declare those members const -- but you weren't using the correct syntax to initialize them. 要声明类的不可变属性,可以声明这些成员const - 但是您没有使用正确的语法来初始化它们。 Given: 鉴于:

class Foo
{ 
  const int mImmutableN;
public:
  Foo();
}; 

You cant initialize mImmutableN in the body of the constructor; 你不能在构造函数的主体中初始化mImmutableN ; it must be initialized in the constructor's initialization list: 它必须在构造函数的初始化列表中初始化:

Foo::Foo()
:
  mImmutableFoo (42)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM