override c++ virtual method

54,003

Solution 1

You just have to make sure that the methods have the same signature (including const/mutable modifiers and argument types). You can use a pure virtual definition to provoke compiler errors if you fail to override the function in a subclass.

class parent {
public:
  // pure virtual method must be provided in subclass
  virtual void handle_event(int something) = 0; 
};

class child : public parent {
public:
  virtual void handle_event(int something) {
    // new exciting code
  }
};

class incomplete_child : public parent {
public:
  virtual void handle_event(int something) const {
    // does not override the pure virtual method
  }
};

int main() {
  parent *p = new child();
  p->handle_event(1); // will call child::handle_event
  parent *p = new incomplete_child(); // will not compile because handle_event
                                      // was not correctly overridden
}

Solution 2

If you can use C++11 features in your compiler then overrides can be tagged as so with the override special identifier:

 float compute() override;

The above line in a derived class will cause a compiler error as the function does not override a member function in the base (incorrect signature, missing argument). But note that this must be done in each derived class, it is not a solution that you can impose from the base class.

From the base class you can only force the override by making the function pure virtual, but that changes the semantics. It does not avoid problems while overriding, but rather forces overriding in all cases. I would avoid this approach, and if you are to follow it and there is a sensible implementation for the base type, make the function virtual and provide a definition so that your derived classes's implementation can just call the functions the base type (i.e. you force the implementation, but in the simplest cases it will just forward the call to the parent)

Share:
54,003
shn
Author by

shn

Updated on July 05, 2022

Comments

  • shn
    shn almost 2 years

    I have a class template where some methods are defined as virtual to give the ability for the user of my class to give an implementation for them in his derived class. Note that in my template class there is some non-virtual methods that makes use of the virtual one (a virtual class that should return a value is called in a non-virtual class).

    Can you give me a simple example of a correct code where the virtual method of the parent class should return a value (but it's implementation is provided in a child class) and the value returned by the virtual method in the parent class is used in other methods of that class. Because I saw somewhere (for example here: Safely override C++ virtual functions) that this can cause some problems and the user defined method will note override the virtual method of the parent class.

    Note: I program with Code::Blocks using g++ compiler.

    EDIT: as requested here a simple example of what I want:

    template<typename T>
    class parent {
    public:
      // Public methods that user can call
      int getSomething(T t);
      void putSomething(T t, int x);
    
      // public method that user should implement in his code
      virtual float compute(T t) { }
    
      // protected or private methods and attributes used internally by putSomething ...
      float doComplexeThings(...); // this can call
    };
    

    The method compute() should be implemented by the user (the child class). However, this method compute() is called by putSomething() and doComplexeThings() for example.

  • Alok Save
    Alok Save over 12 years
    Covariant return types are allowed for virtual methods.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas over 12 years
    @als, just a small detail to 'covariant return types are allowed', covariant types are only allowed if the function returns a reference or pointer. I know that you know this, but I am leaving this comment for others.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas over 12 years
    @mschneider The problem with your answer is that you are forcing a change in the semantics just to avoid a potential problem. In the original case the function was present but could be overloaded, in your proposed solution it must be overloaded. This does not tackle errors while overloading an existing function, but rather forces derived classes to overload.
  • kfmfe04
    kfmfe04 over 12 years
    +1 for an interesting C++11 feature - helpful for ensuring that your method really is overriding something