I'm trying to inherit from a hierarchy of abstract base classes, using an equivalent hierarchies of implementation classes. The only way I've figured out how to do it is using virtual inheritance.
/* Foo Interface */
class IFoo
{
public:
virtual void foo() = 0;
virtual ~IFoo() = default;
};
/* Bar Interface */
class IBar : virtual public IFoo
{
public:
virtual void bar() = 0;
virtual ~IBar() = default;
};
/* A specialized Foo */
class SpecificFoo : virtual public IFoo
{
public:
void foo() override { std::cout << "Foo!\n"; }
};
/* A specialized Bar */
class SpecificBar : virtual public IBar, virtual public SpecificFoo
{
public:
void bar() override { std::cout << "Bar!\n"; }
};
int main()
{
SpecificBar b;
b.bar();
return 0;
}
Although I'm not entirely opposed to using virtual inheritance, I'm not sure that it's the correct approach. Having to use dynamic_cast seems like something to avoid if possible. Is there a better way to do this?
I've tried something with an adapter pattern, but copy/pasting wrapper methods is getting out of hand.
I don't see the benefit of making IBar a sub-type of IFoo from the posted code.
IMO, it will be better to remove that inheritance.
/* Foo Interface */
class IFoo
{
public:
virtual void foo() = 0;
virtual ~IFoo() = default;
};
/* Bar Interface */
class IBar
{
public:
virtual void bar() = 0;
virtual ~IBar() = default;
};
/* A specialized Foo */
class SpecificFoo : public IFoo
{
public:
void foo() override { std::cout << "Foo!\n"; }
};
/* A specialized Bar */
class SpecificBar : public IBar
{
public:
void bar() override { std::cout << "Bar!\n"; }
};
/* A specialized Foo and Bar */
class SpecificFooAndBar : public IFoo, public IBar
{
public:
void foo() override { std::cout << "Foo!\n"; }
void bar() override { std::cout << "Bar!\n"; }
};
I came up with a solution that avoids virtual inheritance, but also gets rid of code duplication. I used a template parameter to specify the base class.
/* Foo Interface */
class IFoo
{
public:
virtual void foo() = 0;
virtual ~IFoo() = default;
};
/* Bar Interface */
class IBar : public IFoo
{
public:
virtual void bar() = 0;
virtual ~IBar() = default;
};
/* A specialized Foo */
template <typename _BaseClass>
class SpecificFooTpl : public _BaseClass
{
public:
void foo() override { std::cout << "Foo!\n"; }
};
using SpecificFoo = SpecificFooTpl<IFoo>;
/* A specialized Bar */
class SpecificBar : public SpecificFooTpl<IBar>
{
public:
void bar() override { std::cout << "Bar!\n"; }
};
int main()
{
SpecificFoo f;
SpecificBar b;
f.foo();
b.bar();
return 0;
}
Related
I'm looking for ways to avoid the "call super" code smell. This code smell is present when a subclass is required to invoke the super class's version of a virtual function when re-implementing that function.
class Base
{
public:
virtual void foo(){ ... }
}
class Derived : public Base
{
public:
virtual void foo(){ Base::foo();// required! ... }
}
If inheritance went only a single layer deep, I could use the template method
class Base
{
public:
void foo(){ ... ; foo_impl(); }
protected:
virtual void foo_impl(){}
}
class Derived : public Base
{
protected:
virtual void foo_impl(){ ... }
}
But if I need to subclass Derived, I'm back where I started.
I'm considering a registration approach.
class Base
{
public:
Base()
{
_registerCallback( [this](){ _baseFoo(); } );
}
void foo()
{
for( auto f : _callbacks )
f();
}
protected:
void registerCallback( std::function<void()> f )
{
_callbacks << f;
}
private:
void _baseFoo() { ... }
std::list< std::function<void()> > _callbacks;
}
class Derived : public Base
{
public:
Derived()
{
_registerCallback( [this](){ _derivedFoo(); } );
}
private:
virtual void _derivedFoo(){ ... }
}
Is there a more standard approach? Any problems with or improvements to this approach?
Use of
class Derived : public Base
{
public:
virtual void foo(){ Base::foo();// required! ... }
}
is the best approach IMO. I am not sure why you would consider that "code smell".
The potential for error is higher in the last approach you suggested.
It's easier to detect a missed call to Base::foo().
If all the classed derived from Base need to implement what Base::foo() does, it's better that the common code be in Base::foo(). The derived classes simply need to make the call.
For what it's worth, we use the pattern at my work a lot and it has proven to be robust over 20+ years of usage.
You can continue using template methods all the way down if you introduce new virtual member function on each level and override it on next one:
template <typename> struct tag {};
class Base
{
public:
void foo() { ... ; foo_impl(tag<Base>{}); }
protected:
virtual void foo_impl(tag<Base>) {}
};
class Derived1 : public Base
{
protected:
virtual void foo_impl(tag<Base>) override final { ... ; foo_impl(tag<Derived1>{}); }
virtual void foo_impl(tag<Derived1>) {}
};
class Derived2 : public Derived1
{
protected:
virtual void foo_impl(tag<Derived1>) override final { ... ; foo_impl(tag<Derived2>{}); }
virtual void foo_impl(tag<Derived2>) {}
};
class Derived3 : public Derived2
{
protected:
virtual void foo_impl(tag<Derived2>) override final { ... ; foo_impl(tag<Derived3>{}); }
virtual void foo_impl(tag<Derived3>) {}
};
If you dislike tag dispatch you can just give methods different names instead, perhaps something like foo_impl_N.
I consider all this overengineering.
chris mentioned a primary concern regards childs not calling their parent's corresponding member functions, this gives an idea about fixing that part:
#include <cassert>
class Base {
public:
void foo() {
foo_impl();
assert(base_foo_called && "call base class foo_impl");
}
protected:
virtual void foo_impl() { base_foo_called = true; }
private:
bool base_foo_called = false;
};
class DerivedFine : public Base {
protected:
void foo_impl() override {
Base::foo_impl();
}
};
class DerivedDerivedFine : public DerivedFine {
protected:
void foo_impl() override {
DerivedFine::foo_impl();
}
};
class DerivedDerivedNotFine : public DerivedFine {
protected:
void foo_impl() override {}
};
int main() {
DerivedFine foo;
foo.foo();
DerivedDerivedFine bar;
bar.foo();
DerivedDerivedNotFine baz;
baz.foo(); // this asserts
}
CRTP can solve everything.
For each foo method, you implement an empty non-virtual foo_before() that does nothing in your CRTP helper.
CRTP helper takes a derived and a base. Its virtual void foo() invokes static_cast<Derived*>(this)->foo_before() then Base::foo() then after_foo().
struct Base {
virtual void foo() { std::cout << "foo\n"; }
virtual ~Base() {};
};
template<class D, class B=Base>
struct foo_helper:B {
virtual void foo() {
static_cast<D*>(this)->before_foo();
this->B::foo();
static_cast<D*>(this)->after_foo();
}
private:
void before_foo() {}; void after_foo() {};
};
struct Derived1 : foo_helper<Derived1> {
void before_foo() { std::cout << "before1\n"; }
};
struct Derived2 : foo_helper<Derived2> {
void before_foo() { std::cout << "before2\n"; }
void after_foo() { std::cout << "after2\n"; }
};
struct DoubleDerived : foo_helper<DoubleDerived, Derived2> {
void after_foo() { std::cout << "even more after\n"; }
};
int main() {
std::cout << "---- Derived1\n";
Derived1 d1;
d1.foo();
std::cout << "---- Derived2\n";
Derived2 d2;
d2.foo();
std::cout << "---- DoubleDerived\n";
DoubleDerived dd;
dd.foo();
}
Live example.
Output:
---- Derived1
before1
foo
---- Derived2
before2
foo
after2
---- DoubleDerived
before2
foo
after2
even more after
Here's an idea inspired by this answer
The idea is to use the fact that constructors and destructors of a struct / class provides a sort of "pre/post function calling" mechanism that gets inherited. So instead of doing the pre/post function calls in the virtual method itself, we can use a functor and define the pre/post function call in the constructor / destructor. That way, functors that inherit from the base functor will inherit the pre/post function call.
Code
struct BasePrePostFunctor
{
BasePrePostFunctor()
{
printf("Base pre-func\n");
}
virtual void operator()()
{
printf("Base Main func\n");
}
~BasePrePostFunctor()
{
printf("Base post-func\n");
}
};
struct DerivedPrePostFunctor : BasePrePostFunctor
{
DerivedPrePostFunctor()
{
printf("Derived pre-func\n");
}
void operator()() override
{
printf("Derived main func\n");
}
~DerivedPrePostFunctor()
{
printf("Derived post-func\n");
}
};
class BaseClass
{
public:
virtual void virtual_func()
{
BasePrePostFunctor func;
func();
}
};
class DerivedClass : public BaseClass
{
public:
void virtual_func() override
{
DerivedPrePostFunctor func;
func();
}
};
int main(int argc, char** argv)
{
DerivedClass derived;
derived.virtual_func();
};
Output
Base pre-func
Derived pre-func
Derived main func
Derived post-func
Base post-func
This code can help to understand, what I want:
class Base
{
public:
void foo()
{
print("Base\n");
Derived::foo(); // smth like that.
}
};
class Derived:public Base
{
public:
void foo()
{
print("Derived\n");
}
}
int main()
{
Derived bar;
bar.foo();
return 0;
}
Any ideas ?
P.S. very bad idea is pass to foo pointer of derived.
You would need the base class to hold some short of a handle of it's derived class like the example below:
template<typename T>
class Base {
T *handle;
protected:
Base(T *_handle) : handle(_handle) {}
public:
void foo() {
std::cout << "Base" << std::endl;
handle->foo();
}
};
class Derived : public Base<Derived> {
public:
Derived() : Base(this) { }
void foo() { std::cout << "Derived" << std::endl; }
};
LIVE DEMO
Adapted from here:
class Derived:public Base
{
public:
void foo()
{
Base::foo();
print("Derived\n");
}
}
int main()
{
Derived bar;
bar.foo();
}
seems to be what you want.
You can declare an abstract method in Base which sub-classes have to override, but you call it although it is abstract. This called Template Method:
class Base
{
public:
void foo() {
print("Base\n");
_foo();
}
private:
virtual void _foo() = 0;
};
class Derived: public Base
{
virtual void _foo() {
print("Derived\n");
};
};
Another alternative would be to make Base a template and pass the Derived type:
template<class D>
class Base
{
public:
void foo() {
print("Base\n");
D::foo();
}
};
class Derived: public Base<Derived>
{
public:
void foo() {
print("Derived\n");
};
};
I would usually go with the first approach as it is easier to grasp and also works with more than one level of inheritance.
Something like
class Derived : public Base
{
public:
void foo()
{
Base::foo();
printf("Derived\n");
}
};
then
int main()
{
Derived bar;
bar.foo();
return 0;
}
OR
int main()
{
Derived bar;
bar.Base::foo();
bar.Derived::foo();
return 0;
}
You are describing static polymorphism, which is solved by CRTP:
template <typename DERIVED>
class Base
{
public:
DERIVED & derived() { return *static_cast<DERIVED *>(this); }
void foo()
{
print("Base\n");
derived().foo();
}
};
class Derived:public Base<Derived>
{
public:
void foo()
{
print("Derived\n");
}
};
You need a virtual function.
#include "windows.h"
#include <iostream>
class Base
{
public:
virtual void foo() const
{
std::cout << "Base\n";
}
};
class Derived : public Base
{
public:
void foo() const
{
Base::foo();
std::cout << "Derived\n";
}
};
void main()
{
Derived d; // call Base::foo on this object
d.foo();
system("pause");
}
I am experiencing a problem where a derived class does not have it's own version of a function called when it is called from a base class pointer. To better explain the classes are defined as below
Class Foo
{
public:
Foo();
virtual ~Foo();
virtual void Event();
}
//-----------------------
Class FooBar : public Foo
{
public:
FooBar();
virtual void Update() = 0;
virtual void Draw() = 0;
}
//-----------------------
Class FinalFoo : public FooBar
{
public:
FinalFoo();
void Update();
void Draw();
void Event();
}
There are other classes similar to FinalFoo. So I attempt to call Event on a pointer to a Foo object expecting that it would call the derived implementation. However, it would appear that it calls the base class version and that is all
FinalFoo* myThing = new FinalFoo();
Foo* baseThing = myThing;
baseThing->Event(); // I expected this to call FinalFoo::Event()
Assuming the above code is corrected, it actually does call FinalFoo::Event(). below is a complete and compilable example. Note, that it also adds the keyword override in strategic points: I'd bet that adding override in the original code, too (and compiling with a compiler aware of this keyword) would point out that your override isn't one.
#include <iostream>
class Foo
{
public:
virtual ~Foo() {}
virtual void Event() { std::cout << "Foo::Event()\n"; }
};
//-----------------------
class FooBar : public Foo
{
public:
virtual void Update() = 0;
};
//-----------------------
class FinalFoo : public FooBar
{
public:
FinalFoo() {}
void Update() override { std::cout << "FinalFoo::Update()\n"; }
void Event() override { std::cout << "FinalFoo::Event()\n"; }
};
int main()
{
FinalFoo myThing;
Foo* baseThing = &myThing;
baseThing->Event();
}
Is it possible to have a derived class to have two sets of the same virtual functions as the base class? I'm looking to do something like the following. The idea being able to choose between two sets of function pointers.
class Base
{
virtual void func1;
virtual void func2;
};
class Derived: Base
{
float somemember;
void somefunction()
{
Base* func = this->derived_functions1;
}
class derived_functions1
{
virtual void func1()
{
return somemember*100;
}
virtual void func2;
};
class derived_functions2
{
virtual void func1;
virtual void func2;
};
};
class Base
{
public:
virtual void func1();
virtual ~Base(){}
};
struct Impl1 : Base
{
void func1() override {}
};
struct Impl2 : Base
{
void func1() override {}
};
struct Derived : Base
{
Derived(std::unique_ptr<Base> implementation) :
impl(std::move(implementation))
{}
void func1() override { impl->func1(); }
void changeImpl(std::unique_ptr<Base> implementation)
{
impl = std::move(implementation);
}
private:
std::unique_ptr<Base> impl;
};
Not the way you did. But you can make both the inner class derived_functionsX to be themseves public: Base, than have your main Derived to contain a std::unique_ptr<Base> ptryou can set to new derived_functions1 or new derived_functions2
and implement in Derived func1 and func2 to call ptr->func1() and ptr->func2().
For all that to work properly, Base must also have a virtual ~Base() {} otherwise no proper deletion can be done.
In this example, it won't compile, since derived_function1 and derived_functions2 aren't inheriting from Base.
But you could have something like this:
class Base
{
virtual void func1();
virtual void func2();
};
class Wrapper {
public:
Wrapper(int arg)
{
switch(arg)
{
case 1:
b = new derived_functions1;
break;
case 2:
b = new derived_functions2;
break;
default:
cout << "bad value of arg" << arg << endl;
exit(1);
}
}
~Wrapper()
{
delete b;
}
Base* GetClass()
{
return b;
}
private:
Base *b;
class derived_functions1: public Base
{
virtual void func1();
virtual void func2();
};
class derived_functions2: public Base
{
virtual void func1();
virtual void func2();
};
};
Short answer: No. A class can override inherited virtual functions only once.
However, there is a design pattern that exchanges function's behavior on the fly, called Strategy Pattern. In short: the class that has exchangeable behavior has a pointer to a Strategy base class that defines the interface for that behavior. It is populated with concrete Strategy classes. The function that has different behavior just delegates its calls to the Strategy pointer. Here's an example, tailored to your question:
class Base {
public:
virtual void func1() = 0;
virtual void func2() = 0;
virtual ~Base(){}
};
#include <iostream>
#include <memory>
class Derived : public Base
{
struct F1Strategy {
virtual void f1Impl() = 0;
virtual ~F1Strategy() {}
};
struct Impl1 : F1Strategy {
void f1Impl() override { std::cout << "one!\n"; }
};
struct Impl2 : F1Strategy {
void f1Impl() override { std::cout << "two?\n"; }
};
std::unique_ptr<F1Strategy> f1Strategy;
public:
Derived()
: f1Strategy(new Impl1())
{}
void func1() override { f1Strategy->f1Impl(); }
void func2() override {
static std::unique_ptr<F1Strategy> otherStrategy(new Impl2());
f1Strategy.swap(otherStrategy);
}
};
int main() {
std::unique_ptr<Base> pb(new Derived());
pb->func1(); // ==> one!
pb->func2(); //swap
pb->func1(); // ==> two?
pb->func1(); // ==> two?
pb->func2(); //swap
pb->func1(); // ==> one!
}
See it in action: http://ideone.com/zk3UTI
I have an interface and some implementations. But in one implementation i have a specify functionality using only in that implementation.
class Interface
{
virtual void foo() = 0;
}
class D1 : public Interface
{
void foo() {}
}
class D2 : public Interface
{
void foo() {}
void bar() {}
}
So i have a D2::bar() function only in D2, and it specified only for D2 implementation.
What is the right way using OOP to write that kind of stuff?
In my client code i have a call:
Interface* i;
i->foo();
But if it is D2 in "i" i need to call bar() function in some cases.
If you need to call the bar function, you need to have a reference to an object that knows about bar.
So, either you reference a D2 object or your Interface must include a bar function. In the later case, your D1 must implement it as well, but the implementation can be empty or return an error value.
If you insist on using interfaces, you should move bar into a dedicated interface and then let clients use that:
class FooInterface {
public:
virtual void foo() = 0;
};
class BarInterface {
public:
virtual void bar() = 0;
};
class D1 : public FooInterface {
public:
void foo() {}
};
class D2 : public FooInterface,
public BarInterface {
public:
void foo() {}
void bar() {}
};
Your client code needing a bar implementation could then take a BarInterface.
Assuming that you inherit D1 and D2 from Interface. you can use cast to convert base pointer to derived object and use it, provided the base pointer points to D2
If you are prepared to have some generic implementation in your interface you put an empty implementation of bar() in your interface:
class Interface
{
virtual void foo() = 0;
virtual void bar() {}
}
class D1 : public Interface
{
void foo() {}
}
class D2 : public Interface
{
void foo() {}
void bar() {}
}
Now when you call Interface i* = blah; i->bar(); if i is a D1 it does nothing, if i is a D2 it will do something D2 specific.
class _interface
{
virtual void foo() = 0;
};
class _abstract : public _interface
{
public:
_abstract(){}
virtual ~_abstract(){};
virtual void foo() = 0;
int get_type()
{
return i_type;
}
protected:
int i_type;
};
class D1 : public _abstract
{
public:
D1(){
i_type = 1;
}
~D1(){}
void foo() {
// do something
}
};
class D2 : public _abstract
{
public:
D2(){
i_type = 2;
}
~D2(){}
void foo() {
// do something
}
void bar() {
// do something
}
};
int main()
{
D1 d_one;
D2 d_two;
_abstract* ab = &d_one;
cout << ab->get_type() << "D1" << endl;
ab = &d_two;
cout << ab->get_type() << "D2" << endl;
return 0;
}
You can identify which child by get_type(). So, you know what time you can use bar(). I don't what is the best approach.