Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cloning

Reply
Thread Tools

Cloning

 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      07-07-2008
Suppose class B is derived from class A As a public member function
in class B, there is a clone

A* clone() const {return new B(*this);}


When and why would this be preferred to B* clone() const{return new
B(*this);}

Thank you,

Paul Epstein
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-07-2008
wrote:

> Suppose class B is derived from class A As a public member function
> in class B, there is a clone
>
> A* clone() const {return new B(*this);}
>
>
> When and why would this be preferred to B* clone() const{return new
> B(*this);}


I don't see a reason. I would choose the latter version.
Well, one reason I could imagine is that the author might not have known
about covariant return types.

 
Reply With Quote
 
 
 
 
pauldepstein@att.net
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 12:49*pm, Rolf Magnus <ramag...@t-online.de> wrote:
> pauldepst...@att.net wrote:
> > Suppose class B is derived from class A * *As a public member function
> > in class B, there is a clone

>
> > A* clone() const {return new B(*this);}

>
> > When and why would this be preferred to B* clone() const{return new
> > B(*this);}

>
> I don't see a reason. I would choose the latter version.
> Well, one reason I could imagine is that the author might not have known
> about covariant return types.


I don't know what covariant return types mean, either. I will google
them immediately. Feel free to include further comments, though.

Paul Epstein
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 4:03 am, pauldepst...@att.net wrote:
> Suppose class B is derived from class A As a public member
> function in class B, there is a clone


> A* clone() const {return new B(*this);}


> When and why would this be preferred to B* clone()
> const{return new B(*this);}


Almost anytime the base class function returns an A*. (The C++
language supports co-variant return types, so returning a B*
here, even if the base class returns an A*, is legal. Normally,
however, it's not very useful, and only leads to confusion; I'd
avoid it except in exceptional cases.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-07-2008
wrote:
> Suppose class B is derived from class A As a public member function
> in class B, there is a clone
>
> A* clone() const {return new B(*this);}
>
>
> When and why would this be preferred to B* clone() const{return new
> B(*this);}


Well, always. It is always better to specify the return type that
describes the actual type of the object being returned as closely as
possible.

With an issue as generic as this one, there are so many examples when
this might come useful that it is hard to choose just one.

Imagine, for example, that somewhere in your code you'd have to work
with the polymorphic class hierarchy rooted at 'B', possibly with some
specific interface features that originate in 'B' (but not present in
'A'). The code does not care, does not know, and does not need to know
about the rest of the hierarchy. With regard to the above 'clone'
function this is immediately and naturally achievable when the return
type is declared as 'B*'. Without it you'd have to use an explicit cast
to forcefully convert the return value to 'B*' type.

Also, you might need to call 'B::clone' is a completely non-polymorphic
context (like "I know a have a 'B' and I know I will get a 'B' from the
clone function"), where the return type of 'A*' looks simply ridiculous
and requires workarounds to make it work.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
pauldepstein@att.net
Guest
Posts: n/a
 
      07-09-2008
On Jul 8, 6:40*am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> pauldepst...@att.net wrote:
> > Suppose class B is derived from class A * *As a public member function
> > in class B, there is a clone

>
> > A* clone() const {return new B(*this);}

>
> > When and why would this be preferred to B* clone() const{return new
> > B(*this);}

>
> Well, always. It is always better to specify the return type that
> describes the actual type of the object being returned as closely as
> possible.
>
> With an issue as generic as this one, there are so many examples when
> this might come useful that it is hard to choose just one.
>
> Imagine, for example, that somewhere in your code you'd have to work
> with the polymorphic class hierarchy rooted at 'B', possibly with some
> specific interface features that originate in 'B' (but not present in
> 'A'). The code does not care, does not know, and does not need to know
> about the rest of the hierarchy. With regard to the above 'clone'
> function this is immediately and naturally achievable when the return
> type is declared as 'B*'. Without it you'd have to use an explicit cast
> to forcefully convert the return value to 'B*' type.
>
> Also, you might need to call 'B::clone' is a completely non-polymorphic
> context (like "I know a have a 'B' and I know I will get a 'B' from the
> clone function"), where the return type of 'A*' looks simply ridiculous
> and requires workarounds to make it work.
>
> --
> Best regards,
> Andrey Tarasevich


Thanks. However, just to clarify, I don't think you meant "Well,
always", but rather "Well, never."
After starting this thread, I read (from 2004), in reference to

B* clone() const{return new B(*this);}

"Many compilers will not compile this syntax, as this is an exception
to the general rule that you cannot override the return type of a
virtual function."

Paul Epstein
 
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
Cloning ASPNET account Joel Leong ASP .Net 1 06-04-2005 04:00 PM
Problem "cloning" a project... VB Programmer ASP .Net 1 03-28-2005 07:18 PM
cloning =?Utf-8?B?QVZM?= ASP .Net 3 12-14-2004 12:01 PM
hard drive cloning tami Microsoft Certification 1 12-15-2003 06:12 PM
Cloning Possible? ASP .Net 1 11-11-2003 01:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57