vgonisanz vgonisanz - 15 days ago 5
C++ Question

Difference between virtual void funcFoo() const = 0 and virtual void funcFoo() = 0;

I have a declaration in a cpp where a function is like:

virtual void funcFoo() const = 0;


I assume that can be inherited by another class if is declared explicit, but what's the difference between

virtual void funcFoo() = 0;


Is important to me improve my programming and i want to know the difference. I don't want a malfunction caused by a bad inherit.

Thanks in advance.

Answer

The first signature means the method can be called on a const instance of a derived type. The second version cannot be called on const instances. They are different signatures, so by implementing the second, you are not implementing or overriding the first version.

struct Base {
   virtual void foo() const = 0;
};

struct Derived : Base {
   void foo() { ... } // does NOT implement the base class' foo() method.
};