This is not perfect, but I think it works.
(IMHO this is a little overcomplex for me )
#include <Core/Core.h>
using namespace Upp;
#include <Core/Core.h>
using namespace Upp;
class A : MoveableAndDeepCopyOption<A> {
public:
int one = 1;
int two = 2;
A() {}
virtual ~A(){}
virtual int sum() const = 0;
A(const A& a, int) {
one = a.one;
two = a.two;
}
virtual A* clone()const = 0;
};
class B : public A {
public:
int three =3;
//Constructor
B(){}
B(const B& b, int){
one = b.one;
two = b.two;
three = b.three;
}
virtual ~B(){}
//Function
virtual int sum()const{
return one +two +three;
}
virtual B* clone()const {return new B(*this);}
};
class C : public A{
public:
int four =4;
//Constructor
C(){}
C(const C& c, int){
one = c.one;
two = c.two;
four = c.four;
}
virtual ~C(){}
//Function
virtual int sum()const{
return one +two +four;
}
virtual C* clone()const {return new C(*this);}
};
CONSOLE_APP_MAIN
{
ArrayMap<Upp::String, A> myArrays;
myArrays.Create<B>("FirstElement").one = 10;
myArrays.Create<B>("SecondElement").two =0;
myArrays.Create<C>("ThirdElement");
Cout() << myArrays.Get("FirstElement").sum() << EOL; //Correct print 15
Cout() << myArrays.Get("SecondElement").sum() << EOL; //Correct print 4
Cout() << myArrays.Get("ThirdElement").sum() << EOL; //Correct print 7
ArrayMap<Upp::String, A> aCopy;
for (int i = 0; i < myArrays.GetCount(); ++i)
aCopy.Add(myArrays.GetKey(i), pick(myArrays[i].clone()));
myArrays.Clear(); //Clearing the base Array
Cout() << aCopy.Get("FirstElement").sum() << EOL; //If the copying goes well it should print 15 even if base array have been destroyed
Cout() << aCopy.Get("SecondElement").sum() << EOL;//If the copying goes well it should print 4 even if base array have been destroyed
Cout() << aCopy.Get("ThirdElement").sum() << EOL;//If the copying goes well it should print 7 even if base array have been destroyed
}