Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`Base *b = new Base;` vs `Base *b = new Base();` without defining my own constructor

If I don't define my own constructor is there any difference between Base *b = new Base; vs Base *b = new Base(); ?

like image 780
brett Avatar asked Sep 13 '10 03:09

brett


People also ask

What is base in constructor?

base (C# Reference) The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.

Why is base constructor called first?

This is why the constructor of base class is called first to initialize all the inherited members.

How do you create a base class constructor?

A derived Java class can call a constructor in its base class using the super keyword. In fact, a constructor in the derived class must call the super's constructor unless default constructors are in place for both classes.

How do you define a constructor in C++?

Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. Constructor is invoked at the time of object creation.


1 Answers

Initialization is kind of a PITA to follow in the standard... Nevertheless, the two already existing answers are incorrect in what they miss, and that makes them affirm that there is no difference.

There is a huge difference between calling new T and new T() in classes where there is no user defined constructor. In the first case, the object will be default-initialized, while in the second case it will be `value-initialized*. If the object contains any POD subobject, then the first will leave the POD subobject uninitialized, while the second will set each subelement to 0.

struct test {
   int x;
   std::string s;
};
int main() {
   std::auto_ptr<test> a( new test );
   assert( a->s.empty() ); // ok, s is string, has default constructor
                           // default constructor sets it to empty
// assert( a->x == 0 );    // this cannot be asserted, the value of a->x is
                           // undefined
   std::auto_ptr<test> b( new test() );
   assert( b->s.empty() ); // again, the string constructor sets to empty
   assert( b->x == 0 );    // this is guaranteed by *value-initialization*
}

For the long road... default-initialize for a user defined class means calling the default constructor. In the case of no user provided default constructor it will call the implicitly defined default constructor, which is equivalent to a constructor with empty initialization list and empty body (test::test() {}), which in turn will cause the default initialization of each non-POD subobject, and leave all POD subobjects uninitialized. Since std::string has a user (by some definition of user that includes the standard library writer) provided constructor, it will call such constructor, but it will not perform any real initialization on the x member.

That is, for a class with user provided default constructor, new T and new T() are the same. For a class without such a constructor, it depends on the contents of the class.

like image 139
David Rodríguez - dribeas Avatar answered Oct 18 '22 03:10

David Rodríguez - dribeas