【发布时间】:2020-04-15 09:55:05
【问题描述】:
我只是想问一下,在 C++、头文件或构造函数中初始化 const 类成员变量的最佳做法是什么?
谢谢:)
在头文件中:
.h 文件:
class ExampleClass{
public:
ExampleClass();
private:
const std::string& string_member_variable_ = "Sample Text";
}
或
在构造函数中:
.h 文件:
class ExampleClass{
public:
ExampleClass();
private:
const std::string& string_member_variable_;
}
.cpp 文件:
ExampleClass::ExampleClass() : string_member_variable_("Sample Text") {}
【问题讨论】:
标签: c++ constructor header initialization member-variables