- 论坛徽章:
- 0
|
想不到我们的面向对象考试有怎么一道又点难度的题
有几个问题我想问一下大家
代码如下:
#include <iostream>
using namespace std;
class B
{
public:
int i;
virtual void p( )
{
cout << i << " in the B " << endl;
}
};
class D1 : public B
{
public :
void p( )
{
cout << i << " in the D1 " << endl;
}
};
class D2 : private B
{
public:
D2( )
{
B::i = 4;
}
int i;
void p( )
{
cout << B::i << " in the D2 with B " << endl;
cout << i << " in the D2 " << endl;
}
};
int main()
{
B b;
B *pb = &b;
D1 d1;
D2 d2;
d1.i = 2;
d2.i = d1.i + ( b.i = 1 );
pb->p();
pb = &d1;
pb->p();
pb = ( B * )&d2;
pb->p();
return 0;
}
pb = ( B * )&d2; 这一句是什么意思?
pb = &d2 为什么通不过编译?
但是,改class D2 : public B , 就可以通过 ??
高手请解答..
[ 本帖最后由 amaorn 于 2008-1-18 15:08 编辑 ] |
|