Salvankar
Salvankar

Reputation: 101

QAC Warning regarding destructor

I have a class defined like somewhat below where copy constructor and assignment operator are deleted. QAC is throwing a warning that "This class has declared default or deleted special members but no destructor". Why is the warning and how to I solve it?

class A
{
  public:
     static A& getInstance()
     {
        static A AInstance;
        return AInstance;
     }
     A(A const&) = delete;
     void operator=(A const&) = delete;
   private:
     A();
};

Upvotes: 1

Views: 225

Answers (1)

Sonic78
Sonic78

Reputation: 798

The linker warning shows what is wrong:

In function A::getInstance()': <source>:8: undefined reference to A::A()'

The fix is simple:

class A
{
  public:
     static A& getInstance()
     {
        static A AInstance;
        return AInstance;
     }
     A(A const&) = delete;
     A& operator=(A const&) = delete;  //< fix 2

   private:
     A() = default;  //< fix 1 (C++11)
};

Fix 1: Provide the definition (let the compiler do it by using "=default" or use {} for old C++)

Fix 2: see https://en.cppreference.com/w/cpp/language/copy_assignment

Fix 3: (if you want to make it explicit) also delete the move constructor and the move assignment operator.

Notes:

Upvotes: 1

Related Questions