3

I have a base State interface class with virtual default destructor.

class State {
public:
    virtual void event() = 0;

    virtual ~State() = default; // relevant part

    virtual void onCreate() {}
    virtual void onDestroy() {}
    virtual void onActivate() {}
    virtual void onDeactivate() {}
};

And then some classes inheriting from it:

class GameState : public State {
public:
    void event() override;
    // ...
};

class MenuState : public State {
public:
    void event() override;
    // ...
};

Compiler generates default move operations if no copy operation or destructor is user-defined. Compiler generates default copy operations if no move operation is user-defined.

  1. Am I correct that by declaring virtual default destructor I have effectively deleted the default move operations?

  2. Will the move operations work for the deriving classes if the base class had implicitly deleted its move operations, and the base class is just an interface without data members?

  3. Is it really sensible to follow the Rule of 5 here? It seems quite a bloat to explicitly delete or default all 5 special member functions.

weno
  • 804
  • 8
  • 17
  • By the way, is there anything smelly about those empty-definitions methods in base `State` class? They are pass-through (state machine calls them anyway) and deriving classes can decide to override them or not. – weno May 24 '20 at 10:52
  • 1
    yes on all 3. `State` is empty anyway, so both copy and move is a no-op. There is no ill effect. – sp2danny May 24 '20 at 10:56
  • 1
    @sp2danny - what do you mean by "effectively deleted"? They are not really deleted - usual copy operations are called when invoked with rvalue argument. – Myrddin Krustowski May 24 '20 at 11:04
  • 1
    they are not defined. that means the overload resolution will find copy operations – sp2danny May 24 '20 at 13:05
  • 1
    It may be a good idea to explicitly default the copy constructor and the copy assignment operator because the implicit declaration of these constructor is deprecated: http://eel.is/c++draft/depr.impldec – Oliv May 24 '20 at 13:43

0 Answers0