Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default member values best practice

Is it good practice when writing C++11 code to set default values for class members in the header file of the class?

Or is it better to do this in the constructor of the class?

EDIT:

I mean:

foo.h:

#include <string>  using std::string;  class Foo{     private:         string greet = "hello";     public:         Foo(); }; 

VS

foo.cpp (of course with the necessary header file, but without the in-class initialization):

Foo::Foo(){     greet = "hello"; } 

Which one is better and why?

like image 996
Paul Avatar asked Jul 21 '12 18:07

Paul


People also ask

How can we provide a default value for a member of a class?

You can simply provide a default value by writing an initializer after its declaration in the class definition. Both braced and equal initializers are allowed – they are therefore calle brace-or-equal-initializer by the C++ standard: class X { int i = 4; int j {5}; };

Does default constructor initialize members?

Default constructors are one of the special member functions. If no constructors are declared in a class, the compiler provides an implicit inline default constructor. If you rely on an implicit default constructor, be sure to initialize members in the class definition, as shown in the previous example.

What is default var value?

variable_name − This is the name of variable given by user. value − Any value to initialize the variable. By default, it is zero.

Can C++ structs have default values?

When we define a struct (or class) type, we can provide a default initialization value for each member as part of the type definition. This process is called non-static member initialization, and the initialization value is called a default member initializer.


2 Answers

If a class member is always initialized with the same initial value, then you should make the initializer inline, so as to avoid duplication. If the initial value depends on the constructor, then put it in the constructor initializer list. (And never use assignment in the way you did.)

Example:

class Foo {     bool done = false;   // always start like this     int qty;     Bar * p;  public:     Foo()                        : qty(0),              p(nullptr)    { }     Foo(int q, Bar * bp)         : qty(q),              p(bp)         { }     explicit Foo(char const * s) : qty(std::strlen(s)), p(new Bar(s)) { }      // ... }; 

In this hypothetical example, the member done is always required to start as false, so it's best to write the initializer inline. The other two members, qty and p, can be initialized differently in each of three different constructors, so they are initialized inside the constructors' initializer lists.

A curiosum: Note that providing an inline initializer prevents your class from having a trivial default constructor.

like image 58
Kerrek SB Avatar answered Oct 21 '22 05:10

Kerrek SB


It depends whether you need to stay compatible with older C++ compilers .When you are not using C++11 you have to initialize most members (all non-static) in the constructor. Further many people advocate to explicitly initialize every member even if this means explicitly calling the default ctor. Usually you should place implementation details in a cpp file not in the header file, thus an example would be

Example: //foo.h  class Foo{ public:    Foo(); private:   std::vector<int> vect; };  //foo.cpp  Foo::Foo():vect(){ } 

In C++11 you have more choices and in class member initializer will become very handy, especially if you have several cors. Here is a good link for more information: http://www.stroustrup.com/C++11FAQ.html#member-init

After Edit: According to your code you are using C++11. To my knowledge there is only few information on good practice concerning the new possibilities but IMHO In class member initializer are very handy to concentrate initialization in one place, which reduces complexity and typing

like image 42
Martin Avatar answered Oct 21 '22 04:10

Martin