碰到一個代碼。我張貼片段。什麼是運營商=返回?
Animal & Animal::operator=(const Animal & o) {
cout << "assignment operator" << endl;
if(this != &o) {
_type = o._type;
_name = clone_prefix + o._name;
_sound = o._sound;
}
return *this;
}
int main(int argc, char ** argv) {
Animal a;
a.print();
const Animal b("goat", "bob", "baah");
b.print();
const Animal c = b;
c.print();
a = c;
a.print();
return 0;
我的問題:這是他指向當前對象吧?賦值操作符返回Animal類型的對象是否正確?因爲它正在返回*這個?但是它不應該返回對象的地址,因爲它的返回類型是Animal &? 我不理解這一點。
這與你對參考參數不執行'a =&c'的原因是一樣的。 – chris 2014-10-17 03:23:45
如果聲明爲「Animal *」,您將返回該地址。引用與指針不同。 – Barmar 2014-10-17 03:25:39