Monthly ArchiveJanuary 2010
C++ 31 Jan 2010 23:23:58
Snippet: Pointer Usage: Polymorphism
//tinodidriksen.com/uploads/code/cpp/pointer-polymorphism.cpp
#include <iostream> struct A { virtual void foo() { std::cout << "A::foo()" << std::endl; } virtual ~A() { } }; struct B : public A { void foo() { std::cout << "B::foo()" << std::endl; } }; int main() { A *a = new A; a->foo(); delete a; a = new B; a->foo(); delete a; }