混淆了如何调用函数Test [英] Confuse about how the function Test be called

查看:116
本文介绍了混淆了如何调用函数Test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

请参阅我的测试代码:

  #include     stdio.h 
class A
{
public
A( ){}
virtual void Test1(){}
virtual void Test2(){}

};

class B: public A
{
public
B(){}
virtual void Test(){printf( B:\ n };}
virtual void Test1(){}
virtual void Test2(){}
void TestVFunctionNotInVtable(){Test();}
};
class C: public B
{
virtual void Test(){printf( C:\ n);}
};
class D: public C
{

};

int main()
{
B * p = new C;
p-> TestVFunctionNotInVtable();
}







当我调试这个时,我看不到功能测试(首先在Vtable中的B)中声明,但按下Ctrl + F5后它显示C:,所以它实际调用C :: Test,我的问题是为什么函数Test不在vtable中但可以调用它。这是IDE vs2010的问题,功能测试是在vtable但它没有显示它?有人知道吗?

解决方案

这正是虚拟的全部内容。当你创建一个C对象时,它知道它是一个C,即使你用B *指针指向它。



tl; dr:polymorphism


显然这是IDE的限制。我在CodeProject上发现了一篇关于它的文章:

显示vtable时调试 [ ^ ]



变量vt将被解释为调试器监视窗口中指向vt条目的指针,但如果键入vt,3,则可以查看整个数组 在观察窗口。 (参见 http://stackoverflow.com/questions/972511/view-array-in- visual-studio-debugger [ ^ ])





PS:我发现你可以在不改变代码的情况下看到整个vtable,如果你在观察窗口输入:(void **)((*(A *)(& * p))).__ vfptr),3

Hi theres!
Please see my test code :

#include "stdio.h"
class A 
{ 
public: 
	A(){} 
	virtual void Test1(){} 
	virtual void Test2(){}

}; 

class  B : public A 
{ 
public: 
	B(){} 
	virtual void Test(){printf("B:\n");} 
	virtual void Test1(){} 
	virtual void Test2(){}
	void TestVFunctionNotInVtable(){Test();}
}; 
class C : public B
{	
	virtual void Test(){printf("C:\n");} 
};
class D:public C
{

};

int main()
{
	B * p = new C;
	p->TestVFunctionNotInVtable();
}




when i debug this ,i don't see the function Test(first declared in class B ) in the vtable,but after press Ctrl +F5 it shows C:,so it actually call C::Test,my problem is why the function Test isn't in vtable but it can be called . is it the problem of IDE vs2010 ,function Test is in the vtable but it doesn't show it? somebody knows?

解决方案

That is exactly what the "virtual" is all about. When you create a "C" object, it knows that it is a C, even though you point to it with a B* pointer.

tl;dr: polymorphism


Apparently this is a limitation of the IDE. I've found an article on CodeProject about it:
Displaying vtable when debugging[^]

The variable vt will be interpretet as a pointer to a vt entry in the debugger watch window, but you can view the entire array if you type "vt,3" in the watch window. (see http://stackoverflow.com/questions/972511/view-array-in-visual-studio-debugger[^] )


P.S.: I just found out that you can see the entire vtable without changing the code, if you type: "(void**)(((*(A*)(&*p))).__vfptr), 3" in the watch window!


这篇关于混淆了如何调用函数Test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆