1

I'm wondering how it is possible to make use of both a copy constructor and assignment operator from a derived class. It seems that all other questions on this issue involve the base class also having an assignment operator of its own, which my base class is not allowed to have.

The member functions in question are:

SpecialOrder& operator=(const SPecialOrder &source)

onemic
  • 59
  • 1
  • 3
  • 10
  • Before you bark further up this wrong tree, I would suggest reading about [the copy/swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom), as this has the distinct aroma of an invalid expectation from a design flaw. – WhozCraig Apr 04 '14 at 08:27

1 Answers1

1

You will need to handle copying the base class members one way or another.

The standard way is to call the base class assignment operator in the derived class, e.g. Order::operator=(source); (Order::Order(source); is incorrect, in fact your compiler should have told you as much, you cannot call a constructor on an already created object).

Your other option is to use the public / protected methods of the base class to try and set the state, but if you cannot access the base class assignment operator or copy constructor, or they are not defined / deleted, it is likely the base class was never intended to be copied in the first place.

user657267
  • 20,568
  • 5
  • 58
  • 77
  • Order::operator=(source) was indeed the answer to my problem. The program works perfectly now. My question though is, why? There is no assignment operator defined in the base class at all, only in the derived class of SpecialOrder. How is it that I'm able to scope to the Order class and use a member function that belongs to one of its children. I always thought that the children inherited from the parents and not the other way round. – onemic Apr 04 '14 at 08:35
  • 1
    Read the `Implicitly-declared copy assignment operator` section http://en.cppreference.com/w/cpp/language/as_operator – user657267 Apr 04 '14 at 08:38