1

Are there any drawbacks / disadvantages using the default constructor for default initialization for primitive data types?

For example

class MyClass
{
public:
    MyClass(); 

private:
    int     miInt;
    double  mdDouble;
    bool    mbBool;
};

Using this constructor:

MyClass::MyClass() 
  : miInt(int())
  , mdDouble(double())
  , mbBool(bool())
{}

instead of this:

MyClass::MyClass() 
  : miInt(0)
  , mdDouble(0.0)
  , mbBool(false)
{}
1
  • Repeating the type is not needed - just write the parentheses. Commented Feb 10, 2012 at 12:18

2 Answers 2

4

No, and the compiler will most probably generate the same code for both.

With optimization off, the following code is generated:

MyClass::MyClass() 
  : miInt(0)
  , mdDouble(0.0)
  , mbBool(false)
{}
012313A0  push        ebp  
012313A1  mov         ebp,esp 
012313A3  sub         esp,0CCh 
012313A9  push        ebx  
012313AA  push        esi  
012313AB  push        edi  
012313AC  push        ecx  
012313AD  lea         edi,[ebp-0CCh] 
012313B3  mov         ecx,33h 
012313B8  mov         eax,0CCCCCCCCh 
012313BD  rep stos    dword ptr es:[edi] 
012313BF  pop         ecx  
012313C0  mov         dword ptr [ebp-8],ecx 
012313C3  mov         eax,dword ptr [this] 
012313C6  mov         dword ptr [eax],0 
012313CC  mov         eax,dword ptr [this] 
012313CF  fldz             
012313D1  fstp        qword ptr [eax+8] 
012313D4  mov         eax,dword ptr [this] 
012313D7  mov         byte ptr [eax+10h],0 
012313DB  mov         eax,dword ptr [this] 
012313DE  pop         edi  
012313DF  pop         esi  
012313E0  pop         ebx  
012313E1  mov         esp,ebp 
012313E3  pop         ebp  
012313E4  ret           

and

MyClass::MyClass() 
  : miInt(int())
  , mdDouble(double())
  , mbBool(bool())
{}
001513A0  push        ebp  
001513A1  mov         ebp,esp 
001513A3  sub         esp,0CCh 
001513A9  push        ebx  
001513AA  push        esi  
001513AB  push        edi  
001513AC  push        ecx  
001513AD  lea         edi,[ebp-0CCh] 
001513B3  mov         ecx,33h 
001513B8  mov         eax,0CCCCCCCCh 
001513BD  rep stos    dword ptr es:[edi] 
001513BF  pop         ecx  
001513C0  mov         dword ptr [ebp-8],ecx 
001513C3  mov         eax,dword ptr [this] 
001513C6  mov         dword ptr [eax],0 
001513CC  mov         eax,dword ptr [this] 
001513CF  fldz             
001513D1  fstp        qword ptr [eax+8] 
001513D4  mov         eax,dword ptr [this] 
001513D7  mov         byte ptr [eax+10h],0 
001513DB  mov         eax,dword ptr [this] 
001513DE  pop         edi  
001513DF  pop         esi  
001513E0  pop         ebx  
001513E1  mov         esp,ebp 
001513E3  pop         ebp  
001513E4  ret     

As you can see, it's identical.

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

3 Comments

Ok, but why I don't see this in public codes like codeproject?
@jisaak matter of habit I suppose.
Thanks, thats it. How did you generate this list?
4

There is more consistent syntax for creating default objects:

MyClass::MyClass() 
  : miInt()
  , mdDouble()
  , mbBool()
{
}

That is, don't pass anything. Just write T() and the object will be created with default value. It is also consistent with class types (think of POD types)!

1 Comment

@LuchianGrigore: Yes. You can use POD types. In fact, you have to. There is no syntax for POD types, other than T() syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.