1

I implemented the following base class and derived classes respectively

class observerInterface {
public:
    virtual void update(std::string data) = delete;
    virtual const std::string & getUniqueID() = delete;

private:
    //deleting copy constructor and copy operator.
    observerInterface(const observerInterface &) = delete;
    observerInterface & operator=(const observerInterface &) = delete;
};
class observer1 final : public observerInterface {

    static constexpr auto UNIQUE_ID = "observer1";

public:
    void update(std::string data) override;
    const std::string & getUniqueID() override;
};

I am facing the following error :

nondeleted function overrides deleted function "observerInterface::******"C/C++(1789).

The issue is not present when I don't delete the update and getUniqueID methods in the base class.

Any idea why I obtain this error?

I am using MSVC compiler version : 19.16.27032.1

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
Raviteja Narra
  • 458
  • 3
  • 11
  • 7
    If you want to define pure virtual functions, please, use `= 0` instead of `= delete`. – Scheff's Cat Apr 07 '21 at 13:51
  • This works. What is the difference between using =0 and =delete ? – Raviteja Narra Apr 07 '21 at 13:53
  • 2
    "pure virtual" is C++ name for functions that other languages call "abstract". Deleted functions are a completely different concept. – Yksisarvinen Apr 07 '21 at 13:54
  • 2
    `= delete` deletes the method. `= 0` makes it pure. It would be easier to list the ways that they're *the same*. – Silvio Mayolo Apr 07 '21 at 13:54
  • 1
    `= 0` for a virtual function makes it pure (and the class abstract). `= delete` (regardless of virtual or not) makes it deleted. That's the difference. ;-) Deleting a function means to exclude a function from a class if (otherwise) the compiler would generate it itself internally or inherit it (from a base class). – Scheff's Cat Apr 07 '21 at 13:54

1 Answers1

4

Here you are deleting the functions:

virtual void update(std::string data) = delete;    
virtual const std::string & getUniqueID() = delete;

These functions are deleted and their use is disabled by the compiler.

If instead, you intend to force these functions to be overriden, you should instead use:

virtual void update(std::string data) = 0;    
virtual const std::string & getUniqueID() = 0;

which means that you declare the methods to be pure virtual.

A pure virtual function is a virtual function that is not required to have a body implementation (however, it is possible that the class can provide a default implementation to such functions), and thus is required to be implemented by a derived class (which means a pure virtual member function must always be implemented by a derived class that is intended to be the concrete [non virtual] ).

As I understand it, you try to derive your observerInterface class then a pure virtual method is adequate to what you try to do.

More details on deleted functions and virtual functions and default implementation for pure virtual functions and pure virtual implementation.

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
  • 4
    "*A pure virtual function is a virtual function that has no body implementation*" - more accurately, it is a virtual function that is *not required* to have a body, but it *can* have a default body which descendants can call if they want, but the compiler won't complain if that body is missing. "*and thus is required to be implemented by a derived class*" - yes, a pure virtual function MUST be overridden in a derived class. But if the base class *does* provide a default body, the derived class chooses whether it wants to call it or not. – Remy Lebeau Apr 07 '21 at 21:05
  • Thanks @Remy for your excellent ( as always ;) ) review. I added the precision. :) – Pat. ANDRIA Apr 08 '21 at 06:20