|
-
February 1st, 2006, 10:21 AM
#9
Re: Clone function (pure virtual base class)
 Originally Posted by dus
I still cannot call clone() on a derived object with this solution, even if I make clone() virtual in the base class:
Code:
error: No matching function call to derived::clone()
You get this error because derived::clone( data* ) hides all overloaded versions of clone inherited from the base class. Because of C++'s name hiding rules, it's as if base::clone() was not inherited by the derived class! This surprised me too when I first learned about it just recently. It is explained well by Scott Meyers in Effective C++ (Third Edition) -- Item 33: "Avoid hiding inherited names."
The solution is to add a "using" statement in the derived class. Like so:
Code:
class derived : public base
{
derived(data* p_derived_data) : base(p_derived_data)
{};
using base::clone;
virtual base* clone(data* p_clone_data)
{
return new derived(p_clone_data);
}
}
That pulls in the other version of clone() that you did not override, and makes public inheritance work as expected.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|