0
votes

I have an abstract base class that has some virtual methods and a virtual destructor. I want to make the copy constructor and the assignment operator of the base class (and the derived class) as private so I can hide them and the static analysis tool (klockwork) cannot flag it. This is my code snippet:

class Parent: public mainParentClassIDontCareAbout
{
 public:
         Parent (int a);
         virtual ~Parent ();
 protected:
         void doSomething(); 
      .
      .
 private:
          // Hide assignment operator and copy constructor
         Parent (const Parent&) {};
         Parent operator= (const Parent&) {return *this;};

 } // end of parent class definition

class Child : public Parent
{
 public:
         Child (void *x, int y, bool z);
         ~Child ();
 protected:
           .
           .
           .
 private:
          // Hide assignment operator and copy constructor
         Child (const Child&) {};
         Child operator= (const Child&) {return *this;};

 } // end of parent class definition

However, I get a compile time error as:

invalid abstract return type for member function ‘Lib::Parent Lib::Parent::operator=(const Lib::Parent&)’

I want to hide the assignment operator and copy constructor. Is this not a valid behavior when we have a virtual destructor or are there any solutions if I want to have the same scenario ?

1

1 Answers

2
votes

The assignment operator should return a reference:

Parent & operator= (const Parent&) {return *this;}
       ^

and likewise for Child. Alternatively, they could return nothing, if you don't care about chaining assignments.

Yours tries to return a copy of the object which, in the case of Parent is impossible because Parent is abstract and can't be instantiated.