Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I create copy constructor in derived class.

Both base class and derived class has it's own unique members.



The base class itself has its own copy constructor defined.

The workaround I know is to create two separate classes.
Posted
Comments
Jochen Arndt 8-Dec-15 4:40am    
What do you mean by unique members?
The members of the base class are also present in the derived class. If you don't need these in the derived class it would be better to have two classes or even create a third one without the shared members that is used as base for the two others.
Are Riff 8-Dec-15 6:01am    
Thanks, after fiddling around a bit i got it to work. I forgot add function that return the _armor value.

Warrior::Warrior(const Warrior& v):
Human (v),
_armor {v.getArmor()}
{}
Jochen Arndt 8-Dec-15 6:10am    
Thank you for your feedback.

But for the next time you should post the relevant code. In your question you did not mention you that got a linker error. So this time only you could find the answer.
Are Riff 8-Dec-15 6:22am    
Sorry about that. I posted new question though this time is about prefix increment and postfix increment operator of the same project.
I hope you're kind enough to look through it.
Thanks

1 solution

Solution found.
C++
class Warrior : public Human {
private:
	int		_armor;
	int		_defense;
public:
	Warrior();

	Warrior	(const int &id,
			 const int &h,
			 const int &a,
			 const int &def):
		_armor(a),
		Human(id,h)
		{}

	Warrior (const Warrior &rhs):
		Human (rhs),				// this line produces linker error before
		_armor	{v.getArmor()}
{}
//...
	
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900