comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex R. Mosteo" <alejandro@mosteo.invalid>
Subject: [OT] Ada classwide copy vs C++ clone
Date: Wed, 06 Jul 2011 18:30:01 +0200
Date: 2011-07-06T18:30:01+02:00	[thread overview]
Message-ID: <iv22if$9j0$1@dont-email.me> (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!



             reply	other threads:[~2011-07-06 16:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-06 16:30 Alex R. Mosteo [this message]
2011-07-06 16:44 ` [OT] Ada classwide copy vs C++ clone 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
replies disabled

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