Setting variables in the class declaration?


Probably another stupid question, but is there a reason for setting the variable value in the class declaration and not the constructor?

I was always taught [and have always practised] setting member variable values in the constructor as it’s always going to be called when the object is created. I can see that it has definite advantages in that there’s a single point where the value is set, but I’ve always used hash defines for that purpose.

However, given that one of the reasons for taking this course was a fun way to re-acquaint myself with c++ following the implementation of c++11 features I just want to make sure that this isn’t something pertinent to changes in the standard?


P.S. I’ve also got an advanced c++ course lined up, but this looked a lot more fun, so decided to take it first :wink:

In class member initialisation was added in C++11, it should be the same as having it in a default constructor

Thanks, that really helpful :slight_smile:

Have a great everything :smile:


Privacy & Terms