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
[SOLVED] Cloning Array of complexe type [message #53269] Wed, 25 March 2020 16:15 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Hello community,

I'm wondering how can I copy an array of complexe type. See this example :
#include <Core/Core.h>

using namespace Upp;

class A{
	public:
		int one = 1;
		int two = 2;

		virtual ~A(){}
		
		virtual int sum()const =0;
		virtual A* clone()const =0;
};

class B : public A{
	public:
		int three =3;
		
		//Constructor
		B(){}
		B(const B& b){
			one = b.one;
			two = b.two;
			three = b.three;
		}
		virtual ~B(){}
		
		//Function
		int sum()const{
			return one +two +three;
		}
		B* clone()const {return new B(*this);}
};

class C : public A{
	public:
		int four =4;
		
		//Constructor
		C(){}
		C(const C& c){
			one = c.one;
			two = c.two;
			four = c.four;
		}
		virtual ~C(){}
		
		//Function
		int sum()const{
			return one +two +four;
		}
		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(myArrays,myArrays.GetCount()); //not working
	/*ArrayMap<Upp::String, A> aCopy;
	aCopy = Upp::clone(myArrays); //Not working*/
	
	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
}

with this simple exemple, compilation error I get is "error: allocating an object of abstract class type 'A'" which makes sense.
Someone can explain me how can I do the trick and copy my array ?

[Updated on: Wed, 25 March 2020 21:13]

Report message to a moderator

 
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: Fri Mar 29 11:47:45 CET 2024

Total time taken to generate the page: 0.01605 seconds