Search code examples
c++inheritancevisual-c++abstract-classpure-virtual

Visual Studio find un-overriden pure virtual functions


I am designing a class which inherits from a few interfaces, each with many pure virtual functions. I want to be able to instantiate my class so I need to override all of the pure virtual functions it inherits. I would appreciate a way to highlight or display the functions I have not yet overridden in my derived class but I have not yet found one.

I've tried generating a class diagram to visually compare the functions in the derived class and the base classes. However, I'd prefer a more direct and elegant solution, especially as the project grows.

Here's a generalized example:

Foo.h

class Foo {
public:
  virtual int foo1() = 0;
  virtual int foo2() = 0;
  virtual int foo3() = 0;
  virtual int foo4() = 0;
  //...
  virtual int foo90() = 0;
};

Bar.h

class Bar : public Foo {
public:
  int foo1() override {
    return 4;
  }
  int foo2() override {
    //do stuff
  }
  //darn, I lost track of what I still need to implement!
};

Does Visual Studio have a way to automatically display which functions are still not overridden? Alternatively, if there's a compiler option to display an error/warning for each of the abstract functions that are preventing the instantiation of my class then that would be just as helpful.


Solution

  • Let's say you have:

    class Foo {
    public:
        virtual int foo1() = 0;
        virtual int foo2() = 0;
        virtual int foo3() = 0;
        virtual int foo4() = 0;
    };
    
    class Bar : public Foo {
    public:
        int foo1() override {
            return 4;
        }
        int foo2() override {
            //do stuff
        }
    };
    

    And you instanciate Bar:

    int main()
    {
        Bar b;
    }
    

    Visual Studio produces a C2259 compilation error, tells that you cannot instantiate abstract class and highlights ("due to the following member") that foo3() and foo4() are abstract:

    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(115,9): error C2259: 'Bar': cannot instantiate abstract class
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(99): message : see declaration of 'Bar'
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(115,9): message : due to following members:
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(115,9): message : 'int Foo::foo3(void)': is abstract
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(95): message : see declaration of 'Foo::foo3'
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(115,9): message : 'int Foo::foo4(void)': is abstract
    1>C:\Users\Amit\source\repos\Test\Test\Test.cpp(96): message : see declaration of 'Foo::foo4'
    1>Done building project "Test.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========