Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #17225

Re: Template overriding base class virtual function?

From Casey <cartec69@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Template overriding base class virtual function?
Date 2012-07-24 07:23 -0700
Organization http://groups.google.com
Message-ID <d1829331-71a5-4994-a135-2f2b04632ef2@googlegroups.com> (permalink)
References <jum7qg$cdl$1@newscl01ah.mathworks.com>

Show all headers | View raw


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>
{};

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Template overriding base class virtual function? Peter Davis <pfd@pfdstudio.com> - 2012-07-24 09:26 -0400
  Re: Template overriding base class virtual function? Casey <cartec69@gmail.com> - 2012-07-24 07:23 -0700
    Re: Template overriding base class virtual function? Noah Roberts <roberts.noah@gmail.com> - 2012-07-24 11:38 -0700
      Re: Template overriding base class virtual function? Peter Davis <pfd@pfdstudio.com> - 2012-07-25 09:06 -0400

csiph-web