Late binding in compile time

Back to programming

template <typename Derived>
class Base
{
public:
    void f()
    {
        ((Derived*)this)->lateBinded();
    }

protected:
    Base()
    {
    }
    Base(const Base&)
    {
    }
    Base& operator=(const Base&)
    {
        return *this;
    }
    ~Base()
    {
        // You should add a compile-time
        // check here that Derived is
        // really derived from Base.
    }
};

class DerivedOne
    : public Base<DerivedOne>
{
public:
    void lateBinded()
    {
    }
};

class DerivedTwo
    : public Base<DerivedTwo>
{
public:
    void lateBinded()
    {
    }
};

int main()
{
    DerivedOne one;
    DerivedTwo two;

    one.f();
    two.f();

    return 0;
}