我想執行以下代碼:有多個構造函數初始化成員變量調用
#include <iostream>
using namespace std;
class ABC {
private:
int x, y;
public:
ABC(){
cout << "Default constructor called!" << endl;
ABC(2, 3);
cout << x << " " << y << endl;
}
ABC(int i, int j){
cout << "Parameterized constructor called with parameters "<< i << " " << j << "!" << endl;
x = i;
y = j;
cout << x << " " << y << endl;
}
};
int main(){
ABC a;
return 0;
}
我得到以下輸出:
默認的構造被稱爲!
帶參數2 3調用的參數化構造函數!
-858993460 -858993460
不應成員變量與值2和3被初始化?
'ABC(2,3);'創建一個本地速度爲'ABC'的實例。 –
[爲什麼我應該更喜歡使用成員初始化列表?](http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list) – LogicStuff
@πάνταῥεῖ那麼我應該如何在同一個對象上進行更改? – Ashish