Search code examples
c++virtual

finding close member function names in C++


I just had a bug (of my own making!) which manifested as follows. I had class with a virtual member function which I needed to extend by adding an extra parameter for some new use cases. I added it, with default values, such that it didn't break any current calls. What I missed was that there was another class which inherited from this class which had an override of this function that was no longer an override. While I'm aware of the override keyword to avoid this when adding new code, I was wondering is there any way of finding all functions that are lexically close enough to be possible cases of a similar bug. Having done this once there is a possibility that I may have done it at some time in the past and would like to retrospectively check the code base.


Solution

  • Clang has warning flag -Woverloaded-virtual

    struct Base
    {
        virtual void foo(int = 42) {}
    };
    
    struct Derived : Base
    {
        virtual void foo() {} // Oups
    };
    

    Demo

    You might also be interested by clang-tidy with modernize-use-override, to add override and avoid this mistake in any C++11 compliant compiler in the future.