急!虚函数的继承问题!

xzyhero 2003-12-11 09:39:30
请教:在MFC中的类都是从CObject类中派生的,而CObject中的几个虚函数可以在所有派生类中的实例中通过调试工具的“Watch”可以看到vfptr.但是我们知道CCmdTarget类也是从CObject类中派生的,而CCmdTarget类中也有相应的Overridables函数,但是在CCmdTarget的派生类中确不能够看到CCmdTarget类的虚函数,请问是怎么一回事?
...全文
45 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzyhero 2003-12-12
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

class Base1 {
public:
Base1() {
cout << "In Base1" << endl;
cout << "Virtual Pointer = " << (int*)this << endl;
cout << "Address of Vtable = " << (int*)*(int*)this << endl;
cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
cout << endl;
}
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
};

class Base2: public Base1{
public:
Base2() {
cout << "In Base2" << endl;
cout << "Virtual Pointer = " << (int*)this << endl;
cout << "Address of Vtable = " << (int*)*(int*)this << endl;
cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
cout << "Value at Vtable 3nd entry = " << (int*)*((int*)*(int*)this+2) << endl;
cout << "Value at Vtable 4nd entry = " << (int*)*((int*)*(int*)this+3) << endl;
cout << endl;
}

virtual void h() { cout << "Base2::h" << endl; }
virtual void j() { cout << "Base2::j" << endl; }
};

class Drive : public Base2 {
public:
Drive() {
cout << "In Drive" << endl;
cout << "Virtual Pointer = " << (int*)this << endl;
cout << "Address of Vtable = " << (int*)*(int*)this << endl;
cout << "Value at Vtable 1st entry = " << (int*)*((int*)*(int*)this+0) << endl;
cout << "Value at Vtable 2nd entry = " << (int*)*((int*)*(int*)this+1) << endl;
cout << "Value at Vtable 3nd entry = " << (int*)*((int*)*(int*)this+2) << endl;
cout << "Value at Vtable 4nd entry = " << (int*)*((int*)*(int*)this+3) << endl;
cout << endl;
}
virtual void f() { cout << "Drive::f" << endl; }
// virtual void g() { cout << "Drive::g" << endl; }
virtual void h() { cout << "Drive::h"<<endl;}
};

int main() {

int temp;

Drive objDrive;
objDrive.h ();
}
xzyhero 2003-12-12
  • 打赏
  • 举报
回复
恩,jruv说得对,其实在虚函数表中的确有相应的内容。
jruv 2003-12-11
  • 打赏
  • 举报
回复
这只能说明VC调试器也有其局限之处!
xzyhero 2003-12-11
  • 打赏
  • 举报
回复
英雄所见略同,高手快进来!
xzyhero 2003-12-11
  • 打赏
  • 举报
回复
是啊,郁闷!这样的话和虚函数的一般定义不是冲突了吗?
glacierrr 2003-12-11
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/TopicView1.asp?id=2014140

这个帖子里面的解法好像有问题,我加了class C : public B
还是达不到"这样你就能看到Func3了"!只能看到最基类的虚函数
xzyhero 2003-12-11
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/TopicView1.asp?id=2014140
尽管这个帖子中给出了所谓的答案,但是在实际的查看中还是不能够看到CCmdTarget的虚函数。
phoenixli 2003-12-11
  • 打赏
  • 举报
回复
UP
xzyhero 2003-12-11
  • 打赏
  • 举报
回复
美人?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧