2012-02-28 153 views
0

我正在阅读有关复制构造函数的内容。 任何机构可以告诉我什么是在下面的语句中发生复制构造函数的const对象

class Base { 
public: 
Base() {cout << "Base constructor";} 
Base(const Base& a) {cout << "copy constructor with const arg";} 
Base(Base& a) {cout << "copy constructor with non-const arg"; return a;} 
const Base& operator=(Base &a) {cout << "assignment operator with non-const arg"; return a;} 
} 

void main() 
{ 
    Base a; 
    Base b = Base(); // This is neither calling copy constructor nor assignment operator. 
} 

请告诉我什么是在“基地B =基地()”语句发生。

+0

它调用Base()吗? – user1227804 2012-02-28 12:56:32

回答

0

拷贝构造函数将在三个casess被称为:

When an object is returned by value 
When an object is passed (to a function) by value as an argument 
When an object is thrown 
When an object is caught 
When an object is placed in a brace-enclosed initializer list 

分配opertator将被调用时,下面:

B b; 
b=a; 

所以你的语句:

Base b = Base(); 

不适合任何上述的。

+0

那么这里发生了什么。 Base()创建一个临时对象,是不是指定给b? – user1235206 2012-02-28 13:18:44