2

I am new to OOP in C++. I have a class that doesn't need to run any code neither does it need any parameter when the object is created, should I still write a constructor only for the initialization of the variables.
I can do this:

#include <iostream> class Something { private: const char* msg = "Hewwo World"; // i can initialise here public: void Print() { for (int i = 0; i < 11; ++i) { std::cout << *msg++; } } }; int main() { Something smth; smth.Print(); } 

instead of this:

#include <iostream> class Something { private: const char* msg; public: Something() { msg = "Hewwo World"; } void Print() { for (int i = 0; i < 11; ++i) { std::cout << *msg++; } } }; int main() { Something smth; smth.Print(); } 

both give the same output and the first method is a lot cleaner (according to me). Is there any reason why I should use the second method?

2
  • The two programs are equivalent. Implicitly-defined default constructor in the first example would do the exact same thing as the user-provided one in the second. Commented Aug 1, 2021 at 4:00
  • 1
    The second case would be better using an initialiser list in the constructor. i.e. Something() : msg("Hewwo World") {}. That makes the example equivalent to the first (apart from the fact that the constructor is generated by the compiler in the first case, and the programmer in the second). Rough rule of thumb - if you can achieve the same net effect using a member initialiser versus a subsequent assignment in the constructor body, prefer to use the initialiser. Commented Aug 1, 2021 at 4:32

2 Answers 2

1
const char* msg = "Hewwo World"; // i can initialise here 

To be precise, you aren't initialising there. You're defning a default member initialiser. This initialiser will be used when you initialise an instance without providing an initialiser for that member.

should I still write a constructor only for the initialization of the variables.

Depends on case, but quite often there is no need to do that. Simple classes are often useful as aggregates. Precondition for a class to be an aggregate is that the class has no user declared constructors.

Sign up to request clarification or add additional context in comments.

1 Comment

Side note: Technically the second example performs an assignment of msg rather than an initialization, but I don't think there is an optimizer in general use that wouldn't optimize this down to the exact same thing with a char array. With something more complicated like a class with complicated construction and assignment logic you could see some small differences. No user-defined constructor vs User defined constructor with assignment in body The no constructor version is clearly superior.
1

in this particular code, you are not giving the class any parameters and there is no need of initializing the msg variable in a constructor. so, the constructor is not needed.

use the constructor when you want any variable to be initialized at the moment when the object is created or when you need to give some parameters to the class,then you can use the constructor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.