I'm familiar with inheritance, I'm just not sure if what I want to do is possible. An example probably best describes it:
class Base
{
public:
Base * parent;
virtual void Func()
{
// do some stuff
parent->Func();
}
};
class Derived1 : public virtual Base
{
public:
virtual void Func()
{
// do some stuff
Base::Func();
}
};
class Derived2 : public virtual Base
{
public:
virtual void Func()
{
// do some stuff
Base::Func();
}
};
class DerivedBoth : public Derived1, public Derived2
{
public:
virtual void Func()
{
// do some stuff
Derived1::Func();
Derived2::Func();
Base::Func();
}
};
- If Derived1::Func is called, Base::Func should also get called (as is shown)
- If Derived2::Func is called, Base::Func should also get called (as is shown)
- If DerivedBoth::Func is called, all of Derived1::Func, Derived2::Func, and Base::Func should get called exactly once (as is shown).
Of course, as it stands now Base::Func will get called 3 times per call to DerivedBoth::Func, where I would like Base::Func to get called exacly once per call to Derived1::Func, Derived2::Func, or DerivedBoth::Func. I could accomplish it the following way, but I'd like to avoid using such hacks:
class Base
{
public:
Base * parent;
virtual void Func(bool Unused = true)
{
// do some stuff
parent->Func();
}
};
class Derived1 : public virtual Base
{
public:
virtual void Func(bool CallBase = true)
{
// do some stuff
if ( CallBase ) Base::Func();
}
};
class Derived2 : public virtual Base
{
public:
virtual void Func(bool CallBase = true)
{
// do some stuff
if ( CallBase ) Base::Func();
}
};
class DerivedBoth : public Derived1, public Derived2
{
public:
virtual void Func(bool Unused = true)
{
// do some stuff
Derived1::Func(false);
Derived2::Func(false);
Base::Func();
}
};