0
class Base
{
    public:
    
    virtual void func1()
    {
        std::cout<<"Base func1"<<std::endl;
    }
   //virtual destructor

};

class Derived : public Base
{
    public:
    
    virtual void func1()
    {
        std::cout<<"Derived Base func1"<<std::endl;
    }
    
    virtual void func2()
    {
        std::cout<<"Derived func2"<<std::endl;
    }
};


int main()
{
    Derived *d = new Derived;
    delete d;
}

I want to know if there will be two "vptr"s created for resolving virtual functions, one in the Base class which will be inherited in Derived class object for func1 and other one in the Derived object for func2.

7
  • Have you checked sizes of both classes? wandbox.org/permlink/6znTdbKbkv5Yn6IB. It will not provide a generic answer, but might help. In linked implementation, there is only one vptr in Derived. Commented Apr 3, 2019 at 10:00
  • I have checked the size of the base class object and derived class object, it appears same on my system "8 bytes". But according to me, 2 vptr should be created in derived class object and one in base class object. Commented Apr 3, 2019 at 10:04
  • Why do you think so? If GCC creates just one vptr in Derived and we suppose it's Stdndard-compliant, then the Standard cannot require 2 vptrs. Commented Apr 3, 2019 at 10:06
  • 2
    BTW, terms "vptr", "vtable", and "virtual table" are not at all mentioned in the Standard. The virtual table mechanism is seemingly an implementation issue. Commented Apr 3, 2019 at 10:12
  • @Naresh "should be created" why? Commented Apr 11, 2019 at 3:12

2 Answers 2

1

On my GCC:

std::cout << sizeof(void*) << ' ' << sizeof(Derived) << '\n';
// Prints 8 8

So one vtable pointer is enough. I'd expect most other compilers to behave in the same way.

derived class also has a virtual function which is not present in Base class

Virtual functions added in Derived are probably simply placed at the end of Derived vtable, after functions inherited from Base.

Sign up to request clarification or add additional context in comments.

Comments

0

vtable/vptr issues are platform specific implementation details, not generally covered by std. I can imagine implementations with no in-object vptr at all. But on most platforms I know, exactly one vptr entry serves all purposes for single inheritance and virtual inheritance. However there is no warranty for this to be portable behavior.

17 Comments

"I can imagine How would such impl work? Would that leak memory, in general?
@curiousguy Why should it leak? One possible implementation is a static associative array that keeps a record of <object,type> pairs(static map<void*,type_info*> rtti;).
It would leak if objects are not destroyed properly.
@curiousguy it is irrelevant to vt. The memory mgmt mechanism is responsible for proper destruction and deallocation; whether or not the type is polymorphic is irrelevant.
How is the map cleaned up?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.