Good practice : Default arguments for pure virtual method

c++

Solution

I often wish to use both default parameters and virtual function as you do. The others have rightfully pointed out however that this leads to ambiguity and is generally not a good idea. There is a reasonably simple solution, one that I use. Give your virtual function a different name, make it protected, and then provide a public function with default parameters which calls it.

class Base {
protected:
    virtual void vSomeMethod(const SomeStruct& t ) = 0;
public:
    void someMethod( const SomeStruc& t = 0 )
    { vSomeMethod( t ); }
}

Derived classes simply override `vSomeMethod` and don't worry at all about the default parameters.

Problem

I have created an abstract base class, which has a pure virtual method with default argument. ``` class Base { ... virtual someMethod(const SomeStruct& t = 0) = 0; ... } class Derived : public Base { ... virtual someMethod(const SomeStruct& t = 0); ... } ``` So I would like to know is it a good practice to set the default argument to pure virtual and overall to virtual methods?

Original source