Vtables for a class

Aug 25, 2011 at 7:00pm
Hi

From my understanding , every class that has at least one virtual function , or inherit virtual functions from another class , has its own Vtable.
But what is exactly the Vtable ? as I read , every class has also its own
Vptr (virtual pointer) .

1. How does the vptr work exactly ? assume the I have the following code
1
2
3
4
5
6
7
8
9
10
11
12
class A
{
public:
	virtual void func() { /* some code */ }
	
};

class B : public A
{
public:
	virtual void func2() { /* some more code */}
};

How does the vptr work with both of these classes ?

2. From what I've learned , the order of the Vtable is important . Why is that?
why can't I just write down the virtual methods of each class , without worrying about the order they appear within the .h file ?

thanks , Dave
Aug 25, 2011 at 8:24pm
1. How does the vptr work exactly ?


It depends on the compiler. The common approach (as I understand it) is that the compiler generates several "groups" of function pointers (one group for every polymorphic class -- in your example, there would be a group for A and B).

Each class then has a pointer (vptr) which points to the appropriate group, so that the correct virtual functions can be called.

But again it varies. The compiler is free to get the job done however it sees fit. It's not a detail you should worry about when programming.


From what I've learned , the order of the Vtable is important . Why is that?


It's not important to you. The compiler takes care of all that. You don't need to care about it at all.

why can't I just write down the virtual methods of each class , without worrying about the order they appear within the .h file ?


You can.
Aug 25, 2011 at 8:34pm
Actually , for my upcoming test, I need to write down the Vtables for each class that I get ,
and the main demand is to write the Vtable by order ,that's why I asked . any idea ?

10x
Aug 25, 2011 at 8:59pm
In that case I can't help you.

Is this like a compiler theory course or something? Usually programmers don't need to worry/care about compiler implementation details.
Aug 25, 2011 at 9:06pm
It's a C++ course with all the basics & theory , involves the "Why-s and How-s" of C++ .
If my questions look a little bit weird , I'm sorry :) really
Aug 25, 2011 at 9:07pm
This is where lecturers get you if you haven't attended class...
Aug 25, 2011 at 9:23pm
it's sounds like asking questions in the forum is a bad thing ... knock yourself out
Aug 25, 2011 at 9:27pm
It's not that asking questions is bad, it's just that we can't help you with this problem because it's specific to your course. As I already mentioned, the way compilers do this is varied. Any answer we could give you would probably be different from what your teacher is lookig for.

It sounds like your teacher is looking for something specific. Something he probably covered in class. Since none of us here attended his class we don't know what he's looking for.

Sorry.
Topic archived. No new replies allowed.