Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty override C++11

Having the Observer pattern.

class Observer
{
   virtual eventA()=0;
   virtual eventB()=0;
    ...
   virtual eventZ()=0;
}

The Observer class cannot be changed, but my class is only interested in the event B. Therefore I need to:

class MyObserver{
    eventA() override {}
    eventB() override { /* Do something */ }
    eventC() override {}
    ...
    eventZ() override {}
}

It is overhead to empty-implement all events, specially if you have a policy to always implement in cpp files (except templates obviously).

Does C++11 offer any keyword for that? Like

...
eventC() override = empty;
...

In that way, I wouldn't need to add the empty implementation in the CPP file.

like image 218
FCR Avatar asked Jul 11 '16 08:07

FCR


People also ask

What is override in C++11?

C++11 adds two inheritance control keywords: override and final. override ensures that an overriding virtual function declared in a derived class has the same signature as that of the base class. final blocks further derivation of a class and further overriding of a virtual function.

What override means C?

override Keyword in C++ The function overriding is the most common feature of C++. Basically function overriding means redefine a function which is present in the base class, also be defined in the derived class. So the function signatures are the same but the behavior will be different.

How do you override in C++?

Access Overridden Function in C++ To access the overridden function of the base class, we use the scope resolution operator :: . We can also access the overridden function by using a pointer of the base class to point to an object of the derived class and then calling the function from that pointer.

Do you need to use override C++?

You don't need the override identifier to do this in C++, it simply enforces that you are doing it properly.


2 Answers

What you are looking for doesn't exist.

Anyway, you can do this:

struct Observer {
    virtual ~Observer() = 0;
    virtual void eventA() {}
    virtual void eventB() {}
    // ...
    virtual void eventZ() {}
};

Observer::~Observer() { }

struct MyObserver: Observer {
    void eventB() override { /* Do something */ }
};

Here you have:

  • Observer still abstract (thanks to its destructor), so you cannot instantiate objects of this type

  • A default empty implementation for all of your methods

  • No need to define empty bodies in your derived classes for those methods in which you are not interested

Thus, as a consequence:

int main() {
    // This compiles and works as expected
    Observer *O = new MyObserver;
    // The following line doesn't compile
    // Observer *O = new Observer;
}

Ok, but you said that:

The Observer class cannot be changed

In this case, you can define an intermediate class that is not instantiable from which to derive, as an example:

struct IntermediateObserver: Observer {
    virtual ~IntermediateObserver() = 0;
    void eventA() override {}
    void eventB() override {}
    // ...
    void eventZ() override {}
};

IntermediateObserver::~IntermediateObserver() { }

struct MyObserver: IntermediateObserver {
    void eventB() override { /* Do something */ }
};

From now on, derive all your custom observers from IntermediateObserver and that's all.

like image 180
skypjack Avatar answered Sep 30 '22 17:09

skypjack


Your design violates the Interface segregation principle, stating that no client should be forced to depend on methods it does not use/need. Maybe you should reconsider the design and create several Observer base classes, one for each event?

If you can not change the design, use {}, there is no empty, default or delete for user-written functions.

Does C++11 offer any keyword for that?

No

like image 26
sergej Avatar answered Sep 30 '22 17:09

sergej