有人可以解释为什么在现代 C++ 构造函数语法中对象名称后面有一个分号吗?例如,在构造函数中... rd; {};

问题描述 投票:0回答:1

大约 20 年前我曾经写过一些 C++。我需要重新做一些工作,但事情已经改变了。尽管在 Stroustrup 的 c++ 书中的第四版中进行了搜索,但我确实不理解一些新语法。我正在使用 Apple clang 版本 15.0.0 (clang-1500.1.0.2.5) 目标:arm64-apple-darwin23.3.0 线程模型:posix。在构造函数中,我希望使用 rd{}; 之类的语法来初始化对象。我不明白为什么编译器想要在对象名称后面加一个分号,例如 rd;{};

以下代码编译并运行 - 我只是不明白注释中指出的部分。

#include <iostream>
#include <random>

using namespace std;

class normRand {

public:

    random_device rd;
    mt19937 gen;
    normal_distribution<double>  dist;

    normRand() {
          // The constructor is meant to initialise the Mersenne twister
          // random number generator with a seed from some device and then
          // use this generator to randomly select values from a standard
          // normal distribution.
      
          // Why is there a semicolon after the object name?
          // The compiler says that the object names are expressions - why?
          
          /* I DO NOT UNDERSTAND! */
          rd; {};
          gen;  {rd();};
          dist; {0.0, 1.0;};       
          /* ******************** */
    }
    
    double val() {
      return dist(gen);
    }

};

normRand myNormRand;

int main() {
    cout << myNormRand.val();
    return 0;
}
c++ syntax constructor
1个回答
0
投票

编译器抱怨这样做

rd{};
因为这不是你可以在构造函数体中做的事情。
rd
已被初始化,因此尝试重新初始化它是一个错误。您应该做的是使用类成员初始值设定项来初始化成员,例如

class normRand {

public:

    random_device rd;
    mt19937 gen;
    normal_distribution<double>  dist;

    normRand() : rd{}, gen{rd}, dist{0.0, 1.0;} {}
    
    double val() {
      return dist(gen);
    }

};

原因

rd; {};

comppiled 是因为

rd;
是一个 id 表达式,并且仅计算为
rd
的值,并且
{}
是一个空代码块,后跟
;
的空表达式。

© www.soinside.com 2019 - 2024. All rights reserved.