【发布时间】:2014-04-13 22:49:08
【问题描述】:
我正在尝试实现下面的函数和类,但是 arr[i].x 的输出是错误的。我正确地得到 arr[0].x = 0 和 arr[1].x = 0,但 arr[2].x 不返回 0。有什么想法吗?
class Base {
public:
int x;
};
class Derived : public Base {
public:
int y;
void init(Base *b);
void foo();
};
void Derived :: init(Base *b) {
for( int i = 0; i < 3; ++i) {
b[i].x = 0;
}
}
void Derived :: foo() {
Derived arr[3];
init(arr);
for( int i = 0; i < 3; ++i) {
cout<<"b["<<i<<"] "<<arr[i].x;
}
}
int main()
{
Derived der;
der.foo();
return 0;
}
【问题讨论】:
标签: c++ arrays class oop object