2
class base {
public:
    virtual void fn(){}
};

class der : public base {};

I know that compiler provides a member call VPTR in class which is initialised with the exact VTABLE at run time by constructor. I have 2 questions

1) Which class holds the VPTR. or all the class is having seperate VPTR.

2) When executing statement der d; how VPTR is being resolved at run time?

iammilind
  • 68,093
  • 33
  • 169
  • 336
user966379
  • 2,823
  • 3
  • 24
  • 30

3 Answers3

3

vtable is created for the class that contains virtual function and for the classes derived from it.It means in your program vtable will be created for base class and der class.Each of these vtables would contain the address of virtual function void fn().Now note that der class doesn't contain the definition of void fn(),hence its vtable contains the address of base class's void fn() function.Thus if u make a call like d.fn(); the void fn() function of base class would get executed.

shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
2

Note: a virtual table and a virtual pointer are implementation details, though all the C++ compilers I know use them, they are not mandated by the Standard, only the results are.

To answer your specific question: each instance of a class with virtual methods (either its own, or inherited ones) or a class with (somewhere) a virtual inheritance relationship will need at least one virtual-pointer.

There can be several (when virtual inheritance or multi-inheritance are involved).

In your example, a single virtual pointer is sufficient. However it does not make sense to speak of it as being part of a class. The virtual pointer is part of the instance (object), and lives outsides the classes rules because those apply to the language, and the virtual pointer is an implementation mechanism.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Just to be clear: The vpointer is part of the instance, but the vtable is part of the class, right? – Kerrek SB Sep 29 '11 at 09:08
  • @Kerrek: typically yes. You may be able to share vtables if they're identical, or one is a pre- or suffix of the other. – MSalters Sep 29 '11 at 09:29
0

1) which class holds the VPTR. or all the class is having seperate VPTR.

Every class object has its own vptr if the class is polymorphic (i.e. contains virtual function or has virtual inheritance.) In this case both the classes has virtual function.

2) when executing statement der d; how VPTR is resolve at run time?

You are just declaring the object of der. But even if you call a function then in this case the call to any function is resolved at compile time. Virtual function resolution comes into picture only when the function is called with pointer/reference.

6502
  • 112,025
  • 15
  • 165
  • 265
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Thank you for your answer. You are telling class base is having a member vptr as well as class der is also having vptr. so when object d is instantiated, constructor has to initialise the vptr with the vtable. both class will initialise vptr with thier respective VTALE. – user966379 Sep 29 '11 at 08:47
  • Virtual inheritance means something else in C++ ... apparently you are using it as "inherits a virtual function". – 6502 Sep 29 '11 at 08:48
  • @6502, In the case of `virtual` inheritance the object *may* contain `vptr`. – iammilind Sep 29 '11 at 08:50
  • @iammilind: The C++ standard with polymorphic class means a class that defines or inherits virtual methods (see 10.3). This is unrelated to having a virtual base class. – 6502 Sep 29 '11 at 16:31