user2738748
user2738748

Reputation: 1116

What's the difference between these constructors?

I'd like to know what is the difference between these two constructors:

List<type*> list = List<type*>();

and

List<type*> list;

The container List was written by me and has a user-defined constructor which takes no parameters.

In my opinion the first line is correct and the second one looks like Java. However, both compile. So, what is the difference between these two statements?

Upvotes: 1

Views: 148

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

The first one requires an accessible copy or move constructor while the second one does not.

Consider for example this demontsrative program. If you will not use MS VC++ then the program shall not compile.:)

#include <iostream>

class A
{
public:    
    A() {}
private:    
    A( const A& ) { std::cout << "A( const A & )" << std::endl; }
};              

int main()
{
    A a = A();
}

because the copy constructor is inaccessible even if otherwise the copy operation could be elided.

Also using the first one provides that the corresponding object will be value-initialized while using the second one provides that the corresponding object will be default initialized.

Upvotes: 4

Related Questions