Click here to Skip to main content
15,885,546 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 7:52
professional«_Superman_»5-Nov-10 7:52 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:01
Joseph Dempsey5-Nov-10 8:01 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:02
professional«_Superman_»5-Nov-10 8:02 
GeneralRe: Compiler Error in Template Functions...? Pin
Joseph Dempsey5-Nov-10 8:17
Joseph Dempsey5-Nov-10 8:17 
GeneralRe: Compiler Error in Template Functions...? Pin
«_Superman_»5-Nov-10 8:56
professional«_Superman_»5-Nov-10 8:56 
GeneralRe: Compiler Error in Template Functions...? Pin
Aescleal5-Nov-10 9:12
Aescleal5-Nov-10 9:12 
AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Aescleal5-Nov-10 9:08
Aescleal5-Nov-10 9:08 
AnswerRe: Compiler Error in Template Functions...? [modified] Pin
Paul Michalik6-Nov-10 1:42
Paul Michalik6-Nov-10 1:42 
I wouldn't recommend to do it that way at all.. Let Clonable be what it is, an interface with no implementation, skip the template member completely. In your example you don't need to cast anything, if you use covariant return type for overrides of clone inimplementations of Clonable:

class X : public Cloneable
{
      virtual X* clone() const { return new X(data); }
};

class Y : public X
{
      virtual Y* clone() const { return new Y(dataf, data); }
};

int main(int argc, char* argv[]) 
{            
      X* x = new X(1);
      X* x1 = x->clone();

      Y* y = new Y(2.12, 4);
      Y* y1 = y->clone();

      return 0;
}


As suggested above, usage of an std::auto_ptr is preferable, but only in client code, since otherwise you loose the ability to define covariant return types. I am using a "Cloner" utility for comfortable casts to derived types which looks something like this:

class ClonerUtility {

 template <class TCoVariantClonable>
 static std::auto_ptr<TCoVariantClonable> CloneAs(const Clonable& pToBeCloned) {
  std::auto_ptr<Clonable> tClone(pToBeCloned.clone());
  if (TCoVariantClonable* tCoVariantClone = 
   dynamic_cast<TCoVariantClonable*>(tClone.get())) {
    tClone.reset(0);
    return std::auto_ptr<TCoVariantClonable>(tCoVariantClone);
  }
  return std::auto_ptr<TCoVariantClonable>();
 }

 template<class TCoVariantClonable>
 static std::auto_ptr<TCoVariantClonable> Clone(const TCoVariantClonable& pToBeCloned) {
  return std::auto_ptr<TCoVariantClonable>(pToBeCloned.clone());
 }
};

//
int main(int argc, char* argv[]) 
{  
 std::auto_ptr<X> tX(new X);
 std::auto_ptr<X> tCloneX = ClonerUtility::Clone(*tX);

 Clonable& tPureClonable = *tCloneX;
 std::auto_ptr<X> tOtherCloneOfX = ClonerUtility::CloneAs<X>(tPureClonable);
}


modified on Sunday, November 7, 2010 4:05 AM

QuestionUsing a Message-Only Window to Receive Device Change Notification Messages [modified] Pin
Jim Fell5-Nov-10 5:11
Jim Fell5-Nov-10 5:11 
AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:32
sitebuilderLuc Pattyn5-Nov-10 5:32 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Jim Fell5-Nov-10 5:41
Jim Fell5-Nov-10 5:41 
GeneralRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn5-Nov-10 5:49
sitebuilderLuc Pattyn5-Nov-10 5:49 
AnswerRe: Using a Message-Only Window to Receive Device Change Notification Messages Pin
Luc Pattyn8-Nov-10 12:17
sitebuilderLuc Pattyn8-Nov-10 12:17 
QuestionWhy IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 2:17
professionalChesnokov Yuriy5-Nov-10 2:17 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 3:32
mveCPallini5-Nov-10 3:32 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 3:55
professionalChesnokov Yuriy5-Nov-10 3:55 
QuestionRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
CPallini5-Nov-10 4:13
mveCPallini5-Nov-10 4:13 
AnswerRe: Why IMediaSeeking fails to seek while filter graph is already running? Pin
Chesnokov Yuriy5-Nov-10 5:50
professionalChesnokov Yuriy5-Nov-10 5:50 
QuestionProverbial good example of CTreeCtrl Pin
federico.strati4-Nov-10 23:56
federico.strati4-Nov-10 23:56 
AnswerRe: Proverbial good example of CTreeCtrl Pin
Sauro Viti5-Nov-10 1:41
professionalSauro Viti5-Nov-10 1:41 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 2:51
federico.strati5-Nov-10 2:51 
GeneralRe: Proverbial good example of CTreeCtrl Pin
CPallini5-Nov-10 3:04
mveCPallini5-Nov-10 3:04 
GeneralRe: Proverbial good example of CTreeCtrl PinPopular
David Crow5-Nov-10 3:09
David Crow5-Nov-10 3:09 
GeneralRe: Proverbial good example of CTreeCtrl Pin
federico.strati5-Nov-10 3:38
federico.strati5-Nov-10 3:38 
JokeRe: Proverbial good example of CTreeCtrl Pin
CPallini5-Nov-10 3:46
mveCPallini5-Nov-10 3:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.