When you use objects of polymorphic types, so those types with at least one virtual method, C++ attaches a few things to the objects:
Vtables to address virtual functions.
RTTI object locators to store the actual types of objects.
Static: Determined by the compiler.
Dynamic: Determined at runtime, stored in RTTI.
From Stroustrup’s 2012 ETAPS keynote address.
Again from Stroustrup’s presentation...
C++: Likely size 4*sizeof(double)
“Pure object-oriented likely cost ...
3*sizeof(reference)+3*sizeof(heap_overhead)+4*sizeof(double)
class Shape {
public:
virtual void draw() { int i=0; }
};
class Circle : public Shape {
public:
virtual void draw() { int i=0; }
};
class Rectangle : public Shape {
public:
virtual void draw() { int i=0; }
};
void drawShape( Shape *sp )
{
sp->draw();
}