I've following code (I've removed some code not important here):
class State {
public:
virtual void enter() = 0;
virtual void update() = 0;
virtual void exit() = 0;
};
class SimpleState : public State {
public:
SimpleState() = default;
SimpleState(const SimpleState&) = default;
SimpleState(SimpleState&&) = default;
virtual ~SimpleState() = default;
public:
void enter() override;
void update() override;
void exit() override;
public:
SimpleState& operator=(const SimpleState&) = default;
SimpleState& operator=(SimpleState&&) = default;
};
I've added default operators due to solve a guideline warning since I've defined the destructor and I need to define also other stuff (rule of 5 if I remember).
If I build it with Visual Studio 2019 by enabling cpp core guidelines, I obtain the following warnings:
SimpleState.hpp: warning C26456: Operator 'SimpleState::operator=' hides a non-virtual operator 'State::operator=' (c.128).
SimpleState.hpp: warning C26456: Operator 'SimpleState::operator=' hides a non-virtual operator 'State::operator=' (c.128).
I want to get rid of it, so I've changed the code in following way:
class State {
public:
virtual void enter() = 0;
virtual void update() = 0;
virtual void exit() = 0;
public:
virtual State& operator=(const State&) = 0;
virtual State& operator=(State&&) = 0;
};
class SimpleState : public State {
public:
SimpleState() = default;
SimpleState(const SimpleState&) = default;
SimpleState(SimpleState&&) = default;
virtual ~SimpleState() = default;
public:
void enter() override;
void update() override;
void exit() override;
public:
SimpleState& operator=(const SimpleState&) override = default;
SimpleState& operator=(SimpleState&&) override = default;
};
But in that case I obtain the following errors:
SimpleState.hpp: error C3668: 'SimpleState::operator =': method with override specifier 'override' did not override any base class methods
SimpleState.hpp: error C3668: 'SimpleState::operator =': method with override specifier 'override' did not override any base class methods
What I'm doing wrong and how can I remove the guideline warning?
I suspect that the C26456 warning in this case is a bug, see also https://developercommunityapi.westus.cloudapp.azure.com/content/problem/617702/c26456-false-positive-with-operator-in-derived-cla.html and https://developercommunity.visualstudio.com/content/problem/228085/c-core-check-false-positive-c26434.html.
The referenced core guideline clause C.128 applies only to virtual member functions, but operator=
is not virtual in your base class and neither does it have the same signature as in the derived class, so there is no reason for it to apply.
Make sure that you really want the destructor declaration in SimpleState
. You have virtual functions in the base class State
, which seems to indicate that you want to use State
polymorphically and that objects might be destroyed through State
pointers, rather than SimpleState
pointers. In that case State
needs to have a virtual destructor declared, not SimpleState
.
If you declare the virtual destructor in State
, then you won't need to declare any destructor in SimpleState
, which will inherit the virtual destructor from State
. Then SimpleState
can follow the rule-of-zero and wont need any of the copy/move assignment operators and copy/move constructors declared, which is the preferred way.