Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template overriding base class virtual function?

Reply
Thread Tools

Template overriding base class virtual function?

 
 
Peter Davis
Guest
Posts: n/a
 
      07-24-2012

I'm trying to write something like a clone() method using the curiously
recurring template pattern. What I'm doing is similar to what's
described here:

http://nerdland.net/2009/06/covarian...-constructors/

but simpler, since I'm not worried about covariance. My clone() method
will always return a Base*. So I have something like

class Base
{
Base(){}
Base(const Base& other){}
virtual Base* clone(){Base b = new Base(); return b;}
};

// This template defines a clone() method with the same signature
// as the one in Base.
template <typename D> class Cloneable
{
public:
virtual Base* clone() {Base b = new D(this); return b;}
};

// I think by inheriting from Cloneable, the clone() method in
// Cloneable() should override the one in Base. But it doesn't.
class Derived : public Base, public Cloneable<Derived>
{
Derived() : Base() {}
Derived(const Derived& other) : Base(other) {}
}

When built with Visual Studio 2008, at least, the debugger shows me that
it's the Base::clone() function that always gets called, instead of the
one derived from Cloneable.

If I make both Base and Cloneable be virtual base classes, this works,
but I don't understand why this is necessary. Why doesn't the template
just override the Base definition of clone()?

Thanks!
-pd

--
----
The Tech Curmudgeon
http://www.techcurmudgeon.com
 
Reply With Quote
 
 
 
 
Casey
Guest
Posts: n/a
 
      07-24-2012
On Tuesday, July 24, 2012 8:26:40 AM UTC-5, Peter Davis wrote:
> If I make both Base and Cloneable be virtual base classes, this works,
> but I don&#39;t understand why this is necessary. Why doesn&#39;t the template
> just override the Base definition of clone()?


Because "the template" (Cloneable<Derived>) doesn't derive from Base. Try:


class Base
{
public:
virtual Base* clone() const = 0;
};

template <typename D> class Cloneable : public Base
{
public:
virtual Base* clone() const {
return new D(*static_cast<const D*>(this));
}
};

class Derived : public Cloneable<Derived>
{};
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      07-24-2012
On Tuesday, July 24, 2012 7:23:35 AM UTC-7, Casey wrote:
> On Tuesday, July 24, 2012 8:26:40 AM UTC-5, Peter Davis wrote:
> &gt; If I make both Base and Cloneable be virtual base classes, this works,
> &gt; but I don&amp;#39;t understand why this is necessary. Why doesn&amp;#39;t the template
> &gt; just override the Base definition of clone()?
>
> Because &quot;the template&quot; (Cloneable&lt;Derived&gt doesn&#39;t derive from Base. Try:


Additionally, you'll want to review what happens when you have the same function defined in two unrelated base classes but don't hide it:

http://crazycpp.wordpress.com/2011/0...d-overloading/
 
Reply With Quote
 
Peter Davis
Guest
Posts: n/a
 
      07-25-2012
On 7/24/2012 2:38 PM, Noah Roberts wrote:
> On Tuesday, July 24, 2012 7:23:35 AM UTC-7, Casey wrote:
>> On Tuesday, July 24, 2012 8:26:40 AM UTC-5, Peter Davis wrote:
>> &gt; If I make both Base and Cloneable be virtual base classes, this works,
>> &gt; but I don&amp;#39;t understand why this is necessary. Why doesn&amp;#39;t the template
>> &gt; just override the Base definition of clone()?
>>
>> Because &quot;the template&quot; (Cloneable&lt;Derived&gt doesn&#39;t derive from Base. Try:


Thank you. The actual code is more complex, as it is the implementation
side of a pimpl pattern, but I'll see if I can adapt this.

>
> Additionally, you'll want to review what happens when you have the same function defined in two unrelated base classes but don't hide it:
>
> http://crazycpp.wordpress.com/2011/0...d-overloading/
>


Thanks. I think a big part of the problem is that I was not able to make
the clone() method on Base be pure virtual. That's because there are
other classes, also abstract, whose default constructors created new
Base objects, so Base could not have any pure virtual methods.

Thank you both!
-pd

--
----
The Tech Curmudgeon
http://www.techcurmudgeon.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Non-template class from a template base class with pure virtual methods vilarneto@gmail.com C++ 2 03-25-2007 08:19 PM
Template specialisation of class with base class template ?! Tim Clacy C++ 12 12-03-2003 05:51 PM
Re: template class derived from non-template base class Matt Graham C++ 0 07-21-2003 09:02 PM



Advertisments