【发布时间】:2014-03-16 09:22:11
【问题描述】:
我想问一下以下两种说法是否有区别:
// C++11
std::vector<int> d {1, 2, 3};
std::vector<int> d = {1, 2, 3};
在这两种情况下,都会调用序列构造函数:
class A {
public:
int a;
A() {
cout << "default constructor" << endl;
};
A(const A& other) {
cout << "copy constructor" << endl;
};
A& operator =(const A& other) {
cout << "assignment operator" << endl;
}
A(std::initializer_list<int> e) {
cout << "sequence constructor" << endl;
};
A& operator =(std::initializer_list<int> e) {
cout << "initializer list assignment operator" << endl;
}
};
int main(int argc, char** argv) {
A a {1, 2}; // sequence constructor
A b = {1, 2}; // sequence constructor
b = {1, 2}; // initializer list assignment operator
}
【问题讨论】: