Virtual function not specified in vptr

The vptr index should display all virtual functions, but in my case only 2 out of 3 virtual functions are displayed.

I provide the full code and screenshots below: -

ClassHeader.h

#include <iostream> using namespace std; // Employee Class class Employee { public : int salary ; Employee(){cout << "Inside CTOR" << endl;} virtual ~Employee() {cout << "Inside DTOR" << endl;} virtual void pay(){cout << "Employee" << endl;} }; // Manager Class class Manager : public Employee { public : virtual void pay(){cout<< "Manager pay" << endl;} virtual void Rank(){cout << "Manager Rank" << endl;} }; // JuniorManager Class class JuniorManager : public Manager { public : virtual void pay(){cout<< "JuniorManager pay" << endl;} virtual void Rank(){cout << "JuniorManager Rank" << endl;} }; 

main.cpp

 #include "ClassHeader.h" void main() { Manager *p = new Manager(); p->pay(); p->Rank(); p = new JuniorManager(); p->Rank(); Employee *pE = dynamic_cast<Employee*>(p); pE->pay(); } 

There are two virtual functions in the Manager class, payment and ranking, but only wages are displayed in vptr.

Can someone please tell me why Rank does not appear, although its a virtual function.

I am using Visual Studio 2008 and with the latest updates on the 64-bit version of Windows 7.

enter image description here

JuniorManager Debugger Screenshot

It also does not show virtual functions. See image below.

enter image description here

+6
source share
5 answers

If you check the class as Employee, because this class does not have Rank (), it will not show Rank () in the vtable. Your screenshot shows the contents of the Employee class.

"Yes, the debugger does not have enough type information to tell how long the array is. Thus, it only displays the first element, if only overridden."

http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/23d4e48e-520e-45b4-8c2f-65c11946d75d

+2
source

Perhaps this is due to the fact that the Manager comes from Employee, but Employee does not have a Rank method. Therefore, when a Manager object calls Rank (), it will never need a virtual table. I bet if you create an instance of JuniorManager Rank, it will be in vtbl.

For a more detailed explanation, see Does a C ++ virtual function call on a derived object via vtable? but I think this is the right reason.

+1
source

I believe this because in your Manager inheritance chain there is no way to override your Rank function, even if it is virtual. In other words, I think your class should know where the redefined virtual functions should send the function call correctly, which means that the implementation must store pointers to virtual functions. If no one can override functions, there is no ambiguity in sending, and there is no reason to keep another pointer in the vtable.

You can verify by doing this the same exercise with the JuniorManager instance: since we need to maintain the pointer to the Manager Rank function in the JuniorManager instances, since we have the overriden Rank function, we should see the virtual pointer in the JuniorManager vtable.

Now I'm curious. Could you check this out for us and report back?

+1
source

I think we see here an odd quirk of Visual Studio, which I cannot explain.

I copied the code in Visual Studio to find out what was going on, and I even tried to add:

JuniorManager *p1 = new JuniorManager();

p1 shown as JuniorManager , which is a subclass of Manager, which is a subclass of Employee.

In all cases, the debugger is fully aware of the exact static type of pointer variables and the exact dynamic type of the objects pointed to, and the exact type of vtable and the exact type of entries in the vtable. However, the vtable is displayed only in the debugger as part of the base class (Employee), and for some reason only known entries for the base class are displayed.

It would be much cooler if you showed the rest of the recordings.

0
source

Linker Linker-> Optimization-> Linker Property / OPT: NOREF. Visual Studio removes references to unused functions (not defined). With this switch builder, these links are not deleted.

0
source

Source: https://habr.com/ru/post/907798/


All Articles