473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cloning

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
Jul 7 '08 #1
5 1476
pa**********@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.

Jul 7 '08 #2
On Jul 7, 12:49*pm, Rolf Magnus <ramag...@t-online.dewrote:
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
Jul 7 '08 #3
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:ja*********@gmail.com
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
Jul 7 '08 #4
pa**********@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
Jul 7 '08 #5
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
Jul 9 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tom | last post by:
I've a problem. I want to clone an object having a list of other objects (and so on :/). Do you know any other way than ICloneable.Clone() implementation for all classes in the way? Help..
3
by: AVL | last post by:
Hi, I've a query in cloning. How cloning is different from creating a new instance of an object.? I suppose cloning also creates a new object and copies the exisitng object's data. Where and when...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
1
by: Dan Dorey | last post by:
I've implemented the ICloneable interface on one of my class. I've written this simple code in two different ways and I think both should work but it's not the case and I'm curious to understand...
0
by: Chris | last post by:
Hi All. I'm cloning a treenode, like so: TreeNode trNewTemp = new TreeNode(); trNewTemp = ((((ICloneable)trTempNode).Clone()) as TreeNode); Now the cloning works- trNewTemp has the same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.