Good practice : Default arguments for pure virtual method

Actually, your code is one of worst possible usage patterns for default parameters, as it involves both inheritance and polymorphic behavior. I support an advice to have a look at related Scott Meyers tip, but here is a short overview:

In case of polymorphic calls, default parameters are used according to declaration for static type, not dynamic one. It is logical as run-time has no idea of default parameters, but breaks any sane assumptions about polymorphic behavior. For example,

#include <cstdio>

class Base
{
        public:
                virtual void f(int a = 1)
                {
                        printf("Base::f(%d)\n", a);
                }
};

class Deriv : public Base
{
        public:
                virtual void f(int a = 2)
                {
                        printf("Deriv::f(%d)\n", a);
                }
};

int main()
{
        Base* a = new Deriv();
        a->f();
        delete a;
        return 0;
}

yields:

Deriv::f(1)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)