Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why not have a virtual ptr per class?

It seems that it’s sufficient to have a virtual ptr per class even when a class is derived from two base classes.

For example,

#include <iostream>
class A{
public:
    void* ptr;
    virtual void printNonOverride() {
        std::cout << "no override" << std::endl;
    }
    virtual void print() {
        std::cout << "hello world!" << std::endl;
    }
};

class C {
public:
    double c;
    virtual void cFunc() {
        std::cout << "C";
    }
};

class B : public A , public C {
};

int main() {
    A a{};
    std::cout << sizeof a << std::endl;
    C c{};
    std::cout << sizeof c << std::endl;
    B b{};
    std::cout << sizeof b << std::endl;
}

The output for this is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

16
16
32

which makes sense because sizeof B = sizeof A + sizeof C. However, this seems a little bit inefficient. Why does B need to allocate 2 virtual pointers? Why not just allocate one that refers to B’s own virtual tables? Then the sizeof B will be 24. Thank you!

>Solution :

Because you can do

B b{};
C* pc = &b;

pc->cFunc();

and pc has to point to something that looks like a C – including starting with a virtual pointer suitable for a C.

B and A can share the same virtual pointer, because the compiler can make it so that B‘s virtual table begins the same way as A‘s virtual table (and then has some extra stuff at the end). But it can’t do this for both A and C at the same time. B‘s virtual table can’t begin the same way as A‘s and begin the same way as C‘s. That’s not possible.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading