PDA

View Full Version : Polymorphism interfaces in C++



ComaWhite
28th June 2009, 20:34
I've been using C++ for quite some time. But I never really asked how it knows to do this.

void
Foo::bar(BarInterface *bary) {
delete *bary;
}

and say somewhere else you have like

class FooBar : public BarInterface {
};

How does it know to call FooBar's constructor if it's cast into BarInterface first before deleting?

wysota
28th June 2009, 21:51
It uses a special pointer table called a vtable where it stores pointers to virtual methods (such as the destructor) associated with an object and the table travels with the object during its whole lifespan.

ComaWhite
1st July 2009, 12:38
lol. I forgot about that. xD. Thanks mate.