comp.lang.ada
 help / color / mirror / Atom feed
* [OT] Ada classwide copy vs C++ clone
@ 2011-07-06 16:30 Alex R. Mosteo
  2011-07-06 16:44 ` Dmitry A. Kazakov
  2011-07-06 21:00 ` Maciej Sobczak
  0 siblings, 2 replies; 6+ messages in thread
From: Alex R. Mosteo @ 2011-07-06 16:30 UTC (permalink / raw)


Hello there,

just trying to get up to speed with my very rusty C++. Sorry for asking what 
is basically a C++ question in an Ada group, but this is one of these "how 
do you do ...", and I guess there's more chance of getting an answer from 
people that know C++ here than in some C++ forum where most likely nobody 
will know Ada. Hence the off-topic flag in subject anyway.

I'm quite used to minimizing pointer use in Ada, and when working with class 
hierarchies, I can copy an object just like this:

X : Some_Base'Class := Derived_Instance;

and X will be properly classwide and will dispatch accordingly when methods 
are invoked.

Now, trying to do the same in C++ has been surprisingly (to me) obscure. 
After much reading about slicing (which cannot accidentally happen in Ada, 
right? One needs explicit view conversions), I'm fully aware that in C++ one 
must use either pointers or references when passing objects around in order 
not to mutilate an instance and keep polymorphism going. Leaving aside how 
scared I'm about dangling references right now, I then hopefully though that 
this:

Base *x = new Base (Derived_Instance);

would achieve the same effect, i.e. getting a copy without slicing. But 
that's not so.

More reading late, my current conclusion is that I need to use the Clone 
pattern and implement a cloning method in every derived class.

So, this is what I'd like to hear confirmed. Am I right? or is there 
something simpler that I'm overlooking?

Thanks!



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-07-07 16:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-06 16:30 [OT] Ada classwide copy vs C++ clone Alex R. Mosteo
2011-07-06 16:44 ` Dmitry A. Kazakov
2011-07-07 14:43   ` Alex R. Mosteo
2011-07-07 16:34     ` Dmitry A. Kazakov
2011-07-06 21:00 ` Maciej Sobczak
2011-07-07 14:55   ` Alex R. Mosteo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox