我想执行以下代码:有多个构造函数初始化成员变量调用
#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