0
class person
{
private:
    std::string name;
    int hungerLevel;
    std::string location;
    int money;
public:
    person() : name(""), hungerLevel(100), location(""), money(0) {}
};

When I initialize my string variables as empty strings in the default constructor using the Initializer list I get the warning "Redundant string initialization".

class person
{
private:
    std::string name;
    int hungerLevel;
    std::string location;
    int money;
public:
    person() : hungerLevel(100), money(0)
    {
        name = "";
        location = "";
    }
};

However, when I initialize my strings as empty strings in the body of the constructor as demonstrated above I don't get any errors. I'm not sure if this warning is only related to IDE that I'm using or if one of them is the proper way/good practice.

nyms1
  • 61
  • 7
  • 1
    In the 2nd snippet you're **assigning** while in the 1st snippet your are **initializing** which is redundant as the warning says in first case. There is no need to initialize the string like `name("")`, when you can just write `name()`. – Jason Apr 17 '22 at 15:16
  • 1
    That's because the strings are not initialized "in the body of the constructor". They have already been initialized. The body of the constructor assigns to them. Assignment is not the same thing as initialization. But, it's a bogus warning, nevertheless. – Sam Varshavchik Apr 17 '22 at 15:17
  • 1
    *When I initialize my string variables as empty strings* -- By default, `std::string` is empty. The code, regardless of the warning, is redundant. – PaulMcKenzie Apr 17 '22 at 15:19
  • I see, so what would be the proper way to assign/initialize those strings as empty strings ```name()``` in the initializer list or assigning as empty strings in the body of the function. Or both of them would be considered bad or unecessary – nyms1 Apr 17 '22 at 15:21
  • 1
    @nyms1 Don't do anything. A `std::string`'s default constructor makes the string empty. `person() : hungerLevel(100), money(0) {}` – PaulMcKenzie Apr 17 '22 at 15:22

1 Answers1

1

The gist of your question has been answered in the comments, but you can avoid a constructor altogether here by declaring your class like this:

class person
{
private:
    std::string name;
    int hungerLevel = 100;
    std::string location;
    int money = 0;
};

This (to me) has the huge advantage that you have the declaration and initialisation of all your member variables in the same place and avoids the danger of forgetting to initialise one in your constructor's initialiser list (which can happen if you add a new member variable, typically). It also means that those variables will be initialised correctly even if you add another constructor, it's a win-win situation.

As other have said, the compiler will emit code to intialise your std::strings automatically (by calling the default constructor), so that will take care of itself.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Asked this style of coding to our professor earlier (Instantiating members in the declaration). He encouraged us to use the default constructor for anything regarding the default constructor. (I have no idea why, I'm still very new to c++), would you consider this style of coding better than using a default constructor in this case? – nyms1 Apr 17 '22 at 15:50
  • 1
    Yes, for the reasons stated. You should ask your professor why he is telling you different. And you're initialising them, not instantiating them (that happens when the object comes into existence). – Paul Sanders Apr 17 '22 at 16:33
  • 1
    One possible reason to avoid the `int money = 0;` style of initialization is if you want your code to compile under pre-C++11 C++ compilers; that feature was introduced in C++11. Whether that's important to you to will depend on what compilers your program needs to support. https://stackoverflow.com/questions/28413154/c-initializing-variables-in-header-vs-with-constructor – Jeremy Friesner Apr 17 '22 at 17:22
  • 1
    @JeremyFriesner I really, really hope that doesn't apply to the OP. – Paul Sanders Apr 17 '22 at 18:03
  • 1
    I see thank you all for the answers helped out alot! – nyms1 Apr 17 '22 at 20:00