c++对象的内存模式
#include <iostream>
using namespace std;
class Obj {
private:
int* a;
public:
int* ga() {
return a;
}
void sa(int* x) {
a = x;
}
};
class In: public Obj {
public:
In() {
int* d = new int[10];
d[0] = 1;
d[1] = 2;
sa(d);
}
};
class St: public Obj {
public:
St() {
int* d = new int[5];
d[0] = 100;
d[1] = 110;
sa(d);
}
};
int main() {
Obj* o = new In();
Obj* a = new St();
cout << o->ga()[0] << endl;
cout << a->ga()[1] << endl;
return 0;
}
上面这段代码的输出结果为:
1
110
说明每个对象都是一个独立的内存空间,而类只决定对象的行为。
WELCOME TO THE MACHINE