使用物体中的覆盖空隙的问题 [英] a problem in use an overrided void in a object
本文介绍了使用物体中的覆盖空隙的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。
我有这些:
Hi.
I have these :
#include <iostream>
using namespace std;
class A;
class A {
public:
virtual void foo() { }
};
class C : public A {
public:
virtual void foo() { cout << "abc?" << endl; }
};
int main()
{
A aObj;
aObj = C();
aObj.foo();
}
但我想在编译后看到abc?。但我没有看到任何东西。我该怎么办 ?我有错误的地方?
谢谢
but I want after compile I see " abc? " . but I dont see any thing. What should I do ? And where I have a mistake ?
Thanks
推荐答案
看看这段代码:
Hi,
Have a look at this piece of code:
A aObj;
aObj = C();
aObj.foo();
aObj
即使你打电话给 C();
而不是 A();
, A
的实例。在 A
中,方法 foo
为空。我做了这个测试:
aObj
is, even because you call C();
and not A();
, an instance of A
. And in A
, the method foo
is empty. I did this test:
#include <iostream>
using namespace std;
class A;
class A {
public:
virtual void foo() { }
};
class C : public A {
public:
virtual void foo() { cout << "abc?" << endl; }
int i;
};
int main()
{
A aObj;
aObj = C();
aObj.i = 0;
aObj.foo();
}
我收到编译错误''A类'没有名为''i'的成员
所以,我肯定 aObj
是一个 A
的实例你可以尝试
I got a compiler error ''class A'' has no member named ''i''
So, I''m sure aObj
is an instance of A
You can try
C aObj;
aObj = C();
aObj.foo();
而不是:
instead of:
A aObj;
aObj = C();
aObj.foo();
希望这会有所帮助。
Hope this helps.
gghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjhghjfg hg
gghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjghghjfg hg
这篇关于使用物体中的覆盖空隙的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文