简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM