2012-02-28 203 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