virtual = overridable, dynamic binding, called at runtime
Without virtual destructor
class Base {Derived destructor will not run.
public:
~Base();
};
Base::~Base()
{}
void unawareOfDerived(Base* base)
{ delete base; }
class Derived : public Base {
public:
~Derived();
};
Derived::~Derived()
{}
int main()
{
Base* base = new Derived();
unawareOfDerived(base);
}
A pure virtual member function is a member function that the base class forces derived classes to provide (give an interface). A pure virtual member function specifies that a member function will exist on every object of a concrete derived class even though the member function is not (normally) defined in the base class.
For example, all objects of classes derived from Shape will have the member function draw(). However, because Shape is an abstract concept, it does not contain enough information to implement draw(). Thus draw() should be a pure virtual member function in Shape.
class Shape {This pure virtual function makes Shape an abstract base class (ABC). Imagine that the "= 0" is like saying "the code for this function is at the NULL pointer."
public:
virtual void draw() = 0;
};
--
check the former post VTable and Size of class (update Jan. 31, 08)
No comments:
Post a Comment