Home » U++ Library support » U++ Core » Deepcopying One container
Re: Deepcopying One container [message #23578 is a reply to message #23574] |
Mon, 02 November 2009 11:14   |
 |
mirek
Messages: 14256 Registered: November 2005
|
Ultimate Member |
|
|
dolik.rce wrote on Mon, 02 November 2009 03:37 | Hi,
I've met following problem with polymorphic classes in One containers. Consider following code:#include <Core/Core.h>
using namespace Upp;
class A{
public:
virtual void DoSmthng(){Cout()<<"I'm A.\n";};
};
class B:public A{
public:
virtual void DoSmthng(){Cout()<<"I'm B.\n";};
};
CONSOLE_APP_MAIN{
One<A> a=new A;
One<A> b=new B;
One<A> c;
c<<=a;
c->DoSmthng();
c<<=b;
c->DoSmthng(); The output of this is It surprised me at first, but after looking in the implementation of operator<<=, I understood that this is to be expected (that is not a bug).
The question is: Is there some workaround to make a copy of One without loosing the information about the type it stores? I mean to make it work same way as if you do c<<=a;
c->DoSmthng();
c<<=b;
c->DoSmthng(); but without a and b beeing picked. Is that even posible?
Thanks for any responses.
Regards,
Honza
|
Surprisingly, yes, we can provide polymorphic copies - by overloading DeepCopyNew.
That can be simplified by using PolyDeepCopyNew, using virtual
Copy method.
In reality, I have never really used polymorphic deep copy, it looks a little bit tricky to me. What is your usage scenario?
Mirek
|
|
|
Goto Forum:
Current Time: Wed Apr 30 19:22:59 CEST 2025
Total time taken to generate the page: 0.02744 seconds
|