I've read that Scott Meyers suggest default behaviour to virutal functions to be:
class base
{
.....
protected:
void vfDefault();
public:
virtual void vf() =0;
};
class d1:public base
{
virtual vf()
{
vfDefault();
....
}
};
class base
{
...
public:
virtual void vf() =0;
}
void base::vf()
{
.....
};
class d1:public base
{
virtual vf()
{
base::vf();
....
}
};
Note that vfDefault()
and vf()
have different access specifiers. Everybody can call base::vf()
, including directly calling base implementation. But only children of the base
can call vfDefault()
. So if you implement default behaviour as a separate protected function you can be sure that external code can't call it directly.