18

I'm updating a class to C++14, and trying to figure out the simplest way to initialize all of the instance variables to zero on construction. Here's what I have so far:

class MyClass {
public:
    int var;
    float* ptr;
    double array[3];
    MyStruct data;
    unique_ptr<MyStruct> smart_ptr;

    MyClass() = default;
    ~MyClass() = default;
}

Is setting the constructor to default the equivalent of doing:

MyClass() : var{}, ptr{}, array{}, data{}, smart_ptr{} {}

... or do I need to init each variable? (I've tried both in Visual Studio and I get zeros either way, but I'm not sure if that's luck or not.)

I'm instancing the class without brackets: MyClass obj;

Chris Nolet
  • 8,357
  • 7
  • 63
  • 91
  • 2
    Man, I was going to give you smug answer but now I'm not certain. See also http://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types – Mikhail Feb 05 '17 at 05:52
  • Hahah :) I know, right! I actually saw that post, but it was really this comment that left me feeling uncertain: http://stackoverflow.com/questions/2417065/does-the-default-constructor-initialize-built-in-types#comment36188015_2418195. So it zero-inits if you don't have a constructor at all, and call `MyClass instance()` with brackets? :| – Chris Nolet Feb 05 '17 at 06:02
  • Logically, setting a constructor to `default` would be expected to default-initialise members. Default-initialisation is not always equivalent to zero-initialisation. – Peter Feb 05 '17 at 07:20

2 Answers2

16

Is setting the constructor to default the equivalent of doing:

MyClass() : var{}, ptr{}, array{}, data{}, smart_ptr{} {}

No. It is not.


The line

MyClass() = default;

is more akin to but not exactly equivalent to:

 MyClass() {}

In either case, using

 MyClass obj;

results in a default-initialized object whose members are default initialized.

However, the difference between them when using

 MyClass obj{};

is that obj will be zero-initialized with the defaulted default constructor while it will be still default initialized with the user provided default constructor.

Community
  • 1
  • 1
R Sahu
  • 200,579
  • 13
  • 144
  • 260
  • typo: int{} should be var{} – AndersK Feb 05 '17 at 06:35
  • Fixed, thanks! I appreciate your answer, too. Am I right in thinking that `MyClass() {}` is a user-provided constructor, whereas `MyClass = default;` is a default constructor, so it would be zero-initialized in some circumstances? http://stackoverflow.com/questions/26699720/value-initialization-default-initialization-or-zero-initialization – Chris Nolet Feb 05 '17 at 07:04
  • @ChrisNolet "default constructor" means a constructor that can be called with no arguments – M.M Feb 08 '17 at 06:28
  • Ahh, right you are, sorry. I meant defaulted constructor. – Chris Nolet Feb 08 '17 at 06:38
3

To make all of your variables zero-initialized on construction, even if the creator did not request it, one way is:

struct MyClassMembers
{
    int var;
    float* ptr;
    double array[3];
    MyStruct data;
    unique_ptr<MyStruct> smart_ptr;
};

struct MyClass : MyClassMembers
{
    MyClass(): MyClassMembers{} {}
};

Then MyClass m; will use MyClassMembers{} to initialize the members.

M.M
  • 134,614
  • 21
  • 188
  • 335
  • unless one of the members has a non defaulted default constructor, as detailed in R Sahu's answer – Danra Feb 08 '17 at 21:57
  • @Danra I'm assuming that OP is fine with a member using its own constructor as defined – M.M Feb 08 '17 at 21:58