Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » [SOLVED] Cloning Array of complexe type
Re: Cloning Array of complexe type [message #53275 is a reply to message #53269] Wed, 25 March 2020 18:34 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3361
Registered: August 2008
Senior Veteran
This is not perfect, but I think it works.
(IMHO this is a little overcomplex for me Sad)

#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
}


Best regards
IƱaki
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: ProgressIndicator issue
Next Topic: Read txt, analyze and convert to csv
Goto Forum:
  


Current Time: Tue May 14 14:25:26 CEST 2024

Total time taken to generate the page: 0.02370 seconds