0

I've written a base class with a virtual pure method and two derived classes that inherit this base class publicly. I used "override" to implement versions of the pure virtual of the abstract base class. In Xcode, and many other IDEs, there's usually a key command to hold down when clicking on a method declaration that will navigate you directly towards its definition. I feel as if that my virtual pure declaration is "linked" to my derived classes overriden methods because I'm able to navigate from the base class to the derived classes by using Command+click on the virtual pure declaration, however, I'm able to freely remove the "override" keyword and compile with no errors, when I always believed that "override" was necessary when implementing a definition for a pure virtual method. Any help explaining this would be great:

Here is the abstract base class:

class MillisecondsHertzValueBase
{
private:

// Removed code to reduce length

public:
    MillisecondsHertzValueBase();
    virtual ~MillisecondsHertzValueBase();

    virtual void calculateValues (const double &input) = 0;

    // Removed code to reduce length
};

Here is one of the derived classes header files, where I can freely remove the "override" keyword:

class MillisecondValues : public MillisecondsHertzValueBase
{
public:
    ~MillisecondValues();
    void calculateValues (const double &input) override;
};

Here is the implementation:

MillisecondValues::~MillisecondValues() {}

void MillisecondValues::calculateValues (const double &input)
{
    // Removed code to reduce length
}
JosephTLyons
  • 2,075
  • 16
  • 39
  • 2
    `override` was added in C++11. Making it necessary would be a pretty big breaking change. – chris Jan 09 '18 at 21:12
  • You mixed 2 concepts, there is a term `override` which means define function with the same signature in derived class and there is a keyword (which was added in C++11). You must override abstract function but do not have to use the keyword to make override explicit (but should). It is similar that you do not have to use `virtual` for overriden but should. – Slava Jan 09 '18 at 21:17
  • You [can](https://stackoverflow.com/q/29145476/27302) make GCC complain if you skip the keyword `override`. Clang only supports warning if you use the keyword in some places but not others. – Daniel H Jan 09 '18 at 21:21

0 Answers0