From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,bfcbd34f9610fd42,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Alex R. Mosteo" Newsgroups: comp.lang.ada Subject: [OT] Ada classwide copy vs C++ clone Followup-To: comp.lang.ada Date: Wed, 06 Jul 2011 18:30:01 +0200 Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit Injection-Date: Wed, 6 Jul 2011 16:30:07 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="M7rP6jhm1IVgvmYaXQFuIA"; logging-data="9824"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX189Wxv+l3NxrdUnrH3ZXKpR" User-Agent: KNode/4.4.10 Cancel-Lock: sha1:V3Q/JCD7FjoY74QiRsYK1HFQaxA= Xref: g2news1.google.com comp.lang.ada:20095 Date: 2011-07-06T18:30:01+02:00 List-Id: 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!