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

* Re: [OT] Ada classwide copy vs C++ clone
  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-06 21:00 ` Maciej Sobczak
  1 sibling, 1 reply; 6+ messages in thread
From: Dmitry A. Kazakov @ 2011-07-06 16:44 UTC (permalink / raw)


On Wed, 06 Jul 2011 18:30:01 +0200, Alex R. Mosteo wrote:

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

AFAIK, no. I created a task in Rosetta Code to illustrate the issue. So far
nobody could solve it in C++ without using clone pattern:

http://rosettacode.org/wiki/Polymorphic_copy

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Ada classwide copy vs C++ clone
  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-06 21:00 ` Maciej Sobczak
  2011-07-07 14:55   ` Alex R. Mosteo
  1 sibling, 1 reply; 6+ messages in thread
From: Maciej Sobczak @ 2011-07-06 21:00 UTC (permalink / raw)


On Jul 6, 6:30 pm, "Alex R. Mosteo" <alejan...@mosteo.invalid> wrote:

> 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?

Yes. This is also why you should *carefully* rethink the requirement
for having this operation in the whole hierarchy. It should not come
up by default.
Also, you will want to use boost::shared_ptr or something similar to
protect yourself from the pointer mess. Raw pointers are to be
avoided, even if pointer-related patterns have to be used.

--
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com



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

* Re: [OT] Ada classwide copy vs C++ clone
  2011-07-06 16:44 ` Dmitry A. Kazakov
@ 2011-07-07 14:43   ` Alex R. Mosteo
  2011-07-07 16:34     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 6+ messages in thread
From: Alex R. Mosteo @ 2011-07-07 14:43 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> On Wed, 06 Jul 2011 18:30:01 +0200, Alex R. Mosteo wrote:
> 
>> So, this is what I'd like to hear confirmed. Am I right? or is there
>> something simpler that I'm overlooking?
> 
> AFAIK, no. I created a task in Rosetta Code to illustrate the issue. So
> far nobody could solve it in C++ without using clone pattern:
> 
> http://rosettacode.org/wiki/Polymorphic_copy

Interesting, I should check this site more often for mi refresher sprees.

Alex.



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

* Re: Ada classwide copy vs C++ clone
  2011-07-06 21:00 ` Maciej Sobczak
@ 2011-07-07 14:55   ` Alex R. Mosteo
  0 siblings, 0 replies; 6+ messages in thread
From: Alex R. Mosteo @ 2011-07-07 14:55 UTC (permalink / raw)


Maciej Sobczak wrote:

> On Jul 6, 6:30 pm, "Alex R. Mosteo" <alejan...@mosteo.invalid> wrote:
> 
>> 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?
> 
> Yes. This is also why you should *carefully* rethink the requirement
> for having this operation in the whole hierarchy. It should not come
> up by default.

Thanks for the confirmation, I'll try to think of alternatives.

> Also, you will want to use boost::shared_ptr or something similar to
> protect yourself from the pointer mess. Raw pointers are to be
> avoided, even if pointer-related patterns have to be used.

I'm already doing this, but good reminder.

Alex.

> 
> --
> Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com




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

* Re: [OT] Ada classwide copy vs C++ clone
  2011-07-07 14:43   ` Alex R. Mosteo
@ 2011-07-07 16:34     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2011-07-07 16:34 UTC (permalink / raw)


On Thu, 07 Jul 2011 16:43:41 +0200, Alex R. Mosteo wrote:

> Dmitry A. Kazakov wrote:
> 
>> On Wed, 06 Jul 2011 18:30:01 +0200, Alex R. Mosteo wrote:
>> 
>>> So, this is what I'd like to hear confirmed. Am I right? or is there
>>> something simpler that I'm overlooking?
>> 
>> AFAIK, no. I created a task in Rosetta Code to illustrate the issue. So
>> far nobody could solve it in C++ without using clone pattern:
>> 
>> http://rosettacode.org/wiki/Polymorphic_copy
> 
> Interesting, I should check this site more often for mi refresher sprees.

... and please, contribute if you find some task interesting (I mean Ada
contributions of course (:-)).

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ 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