I just wanted to ask what's the best practice for initializing const class member variables in C++, in the header file or in the constructor?
Thanks : )
In the header file:
.h file:
class ExampleClass{
public:
ExampleClass();
private:
const std::string& string_member_variable_ = "Sample Text";
}
or
In the constructor:
.h file:
class ExampleClass{
public:
ExampleClass();
private:
const std::string& string_member_variable_;
}
.cpp file:
ExampleClass::ExampleClass() : string_member_variable_("Sample Text") {}