我已經理解了拷貝構造函數的概念,我們使指針具有不同的位置並指向相同的值。C++特殊成員拷貝構造函數
但我想知道爲什麼當我嘗試訪問一個對象的變量的值時,它的值不同於另一個的值。
例如:
#include <iostream.h>
using namespace std;
class myclass {
public:
int a, b;
string c;
};
int main()
{
myclass foo, bar;
foo = bar;
bar.a = 20;
cout << foo.a << endl; // why it is not printing 20 here, as implicit copy
// constructor copy all members from object bar
// to object foo?
}
因爲您在設置該成員之前進行了複製。 – Till 2014-09-01 16:53:50
*「我已經理解了複製構造函數的概念」*,不,你沒有:( – 2014-09-01 16:54:00
當你有指針時,你只會指向指向同一個對象的指針,這裏沒有指針,只有兩個獨立的對象。更改一個對象不會改變另一個對象 – 2014-09-01 16:58:39