Thread: Virtual function and multiple inheritance

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    Virtual function and multiple inheritance

    Hello everyone,


    In the following multiple inheritance sample code, I have tested in class Derived, there are two __vfptr, pointing to the virtual function table for Foo and Goo repectively -- i.e. 8 bytes, 2 pointer on 32-bit machine.

    My questions,

    1. Why two __vfptr is needed? Why not just one?
    2. class Derived has its own virtual method func2, why it does not have its own virtual function table pointer?

    Code:
    class Foo {
    public:
    	virtual int func() {return 1;};
    };
    
    class Goo {
    public:
    	virtual int func() {return 2;};
    };
    
    class Derived: Foo, Goo {
    public:
    	virtual int func2() {return 3;};
    	int increase() {return -1;};
    	int decrease() {return -2;};
    };
    
    int main()
    {
    	Derived d;
    	int size;
    
    	size = sizeof (d); // size is 8, two __vfptr?
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Foo and Goo are unrelated classes with virtual functions. Thus, they both need a vptr.

    When combining them to be part of Derived, they can't change layout. It would break code. Thus, the combined Derived object has both vptrs.

    The Derived object already has two vptrs to choose from. Why would it add yet another?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I agree with your theory, but it does not cover one of my confusions, which is, where is class Derived's own __vfptr (points to its own virtual methods)? From debugging, I can not find it out by adding a new method in Derived which is not in Foo and Goo. Like, we add a new virtual method called func_derived.

    Code:
    virtual int func_derived() {return 4;}

    regards,
    George

    Quote Originally Posted by CornedBee View Post
    Foo and Goo are unrelated classes with virtual functions. Thus, they both need a vptr.

    When combining them to be part of Derived, they can't change layout. It would break code. Thus, the combined Derived object has both vptrs.

    The Derived object already has two vptrs to choose from. Why would it add yet another?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I repeat:
    The Derived object already has two vptrs to choose from. Why would it add yet another?
    See, the way virtual tables work, there is one for every class. Derived doesn't need another vptr - it simply lets its base class vptrs point to its own table.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But here is an interesting question: what if the class has two base classes? Which vptr will it use in this case? I'll admit, I've never studied the vptr table.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's one of the details in the vptr implementation that may very well differ between compilers.

    But typically, I'd say it uses that of the first base class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    I have posted my code and screen snapshot, you can see in Derived class there are two vfptr pointing two different virtual function table.

    See the code and attachment, thanks.

    Any ideas?

    Code:
    class Foo {
    public:
    	virtual int func_foo() {return 1;};
    };
    
    class Goo {
    public:
    	virtual int func_goo() = 0;
    };
    
    class Goo2: public Goo {
    public:
    	virtual int func_goo() {return 2;};
    };
    
    class Goo3: public Goo2 {
    public:
    	virtual int func_goo() {return 2;};
    };
    
    class Derived: Foo, Goo {
    public:
    	virtual int func_foo() {return 3;};
    	virtual int func_goo() {return 2;};
    	virtual int func_derived() {return 4;}
    	int increase() {return -1;};
    	int decrease() {return -2;};
    };
    
    int main()
    {
    	Derived d;
    	Goo2 g2;
    	Goo3 g3;
    	int size;
    
    	size = sizeof (d); // size is 8, two __vfptr?
    	size = sizeof (g2);
    	size = sizeof (g3);
    
    	return 0;
    }
    Quote Originally Posted by CornedBee View Post
    I repeat:


    See, the way virtual tables work, there is one for every class. Derived doesn't need another vptr - it simply lets its base class vptrs point to its own table.

    regards,
    George

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    You can find detailed information in my post #7 when there are two vfptr in one class. I provide code and screen snapshots. It is appreciated if you could add some ideas. :-)

    Quote Originally Posted by Elysia View Post
    But here is an interesting question: what if the class has two base classes? Which vptr will it use in this case? I'll admit, I've never studied the vptr table.

    regards,
    George

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    CornedBee already told you: Derived inherits the vtable pointers from its base classes!
    The debug window shows it as well. Those pointers are part of the Foo and Goo base classes.
    What more do you need to know?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Derived::vftable for Goo
    Derived::vftable for Foo

    How much clearer can the debugger be?



    ... not that it matters. That stuff is interesting if you want to implement a C++ compiler yourself.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Why virtual function func_derived of class Derived is not in virtual table of class Derived? In virtual function theory, all virtual tables should be in vtable.

    Quote Originally Posted by Elysia View Post
    CornedBee already told you: Derived inherits the vtable pointers from its base classes!
    The debug window shows it as well. Those pointers are part of the Foo and Goo base classes.
    What more do you need to know?

    regards,
    George

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    This is what you mentioned before.

    "Derived doesn't need another vptr - it simply lets its base class vptrs point to its own table."

    But if you debug my simlpe program, Derived class's own virtual function func_derived is not in vtable. Why?

    Quote Originally Posted by CornedBee View Post
    Derived::vftable for Goo
    Derived::vftable for Foo

    How much clearer can the debugger be?



    ... not that it matters. That stuff is interesting if you want to implement a C++ compiler yourself.

    regards,
    George

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Thanks Elysia,

    Why virtual function func_derived of class Derived is not in virtual table of class Derived? In virtual function theory, all virtual tables should be in vtable.

    regards,
    George
    Take a look into the vptr of the base classes and see if you can find it there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    I can not find virtual method in vtable func_derived and it is missed in vtable of class Derived. Can you find it or meet with similar issues before -- not all virtual methods are in vtable?

    Quote Originally Posted by Elysia View Post
    Take a look into the vptr of the base classes and see if you can find it there.

    regards,
    George

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I have no idea how it stores the functions the vtables (in case of Derived). They certainly don't show up in the debugger, but they certainly work, so they must be in a vtable. In fact, the assembly shows this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM