Reputation: 1116
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
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