Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi theres!
Please see my test code :
C++
#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?
Posted

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
 
Share this answer
 
Comments
xd zong 14-Jul-13 22:53pm    
i think the virtual call depends on whether the virtual function is in the vtable. here is not in
H.Brydon 14-Jul-13 22:58pm    
If you are asking about TestVFunctionNotInVtable(), it should show up in B, C and D since you implemented it in B. When you call p->TestVFunctionNotInVtable() on the C object, it should see C::Test() (since it is a C object).
xd zong 14-Jul-13 23:15pm    
i don't konw why it should see::Test.
H.Brydon 15-Jul-13 0:16am    
No that is not what I mean. Look at your code for B.

When calling a C object, B::TestVFunctionNotInVtable() will call C::Test().

Stefan_Lang 17-Jul-13 4:02am    
I think he does understand the output. What he doesn't understand is why the function "Test" doesn't show up in the vtable when he inspects the p in the debugger!

I just tested it: When you stop the program before executing the last line, the vtable will show entries for Test1() and Test2(), but not for Test().

I have no explanation for this, other than that it may be an error of the IDE: apparently it only shows entries for those functions declared in the top level class, A.
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!
 
Share this answer
 
v2
Comments
H.Brydon 17-Jul-13 10:35am    
Good find. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900