Human::operator=(std::move(f)) //Call assignment operator of base classaĭoes this class comply with the copy-swap idiom? Is anything missing or overlooked? Rvalue reference to hold temporaries as parameter Now f is an lvalue (since it has a name) that is holding an rvalue. Human::operator=(temp) //<-Design question regarding this at end.Įmployee(employee& f) : human( std::move( f)) //call move constructor of base class If we do not do that the regular constructor of base class will be called.Įmployee(const employee& f) : human(f) //This will call the copy constructor of base classĭuty = new char Įmployee temp = f //Call the copy constructor call the copy constructor of the base class using initialization list Since we provided a custom copy constructor we should also Human temp = f //Call the copy constructor If(this != &f) //Make sure they are not the same Std::swap(lhs.blood_type, rhs.blood_type) īlood_type = new char I also understand that using a char * is not recommended for a C++ project however most of the projects that I have come across use a char* here and there.
#C++ COPY CONSTRUCTOR GAME CODE#
I would like to keep the code to a minimum to serve as a guiding principal for further classes. If you have solid suggestions about exception safety please let me know. I am more interested in making this a generic class. In the following code class employee inherits from class human - The constructors and assignment operators of both the classes are quite similar in manner. Following is my attempt of a copy-swap idiom applied to class constructors and assignment operators. However I could not find this idiom applied to classes that have base classes. I came across this post which explains the copy and swap idiom.