Hi, to clone CClass values to a CClass values_new?
I this line code: objects[size] = new CTest();
create a new object but I need to clone the last object.
how to clone?
I played around with your code (see attached).
Dubugging the objects[] array revealed that the attempted copy actually destroys the 2nd object, with the 2nd array location pointing to the original object (see pic)
I suggest you create your own get/set methods so you can pass the data to the 2nd object and manually clone the values.
int size = 0; objects[size] = new CTest(); //1st Obj objects[size].name = "Alpha"; PrintFormat("objects[%d].name = %s", size, objects[size].name); objects[size+1] = new CTest(); //2nd Obj objects[size+1].name = "Beta"; PrintFormat("objects[%d].name = %s", size+1, objects[size+1].name); objects[size+1] = objects[size]; //Attempt to copy - does not work (now points to 1st object) PrintFormat("objects[%d].name = %s", size, objects[size].name); PrintFormat("objects[%d].name = %s", size+1, objects[size+1].name);
Solved, thanks LanMM my friend. @Arelian Monteiro Maia
//+------------------------------------------------------------------+ //| test.mq5 | //| Copyright 2022, Márcio Andrade | //| https://www.marcioandrade.com.br | //+------------------------------------------------------------------+ //--- version "1.00" #property copyright "Copyright 2022, Márcio Andrade." #property link "https://www.marcioandrade.com.br" #property version "1.00" //+------------------------------------------------------------------+ //| Class | //+------------------------------------------------------------------+ class CTest { public: string name; int number; CTest(void); ~CTest(void); void Clone(CTest &ref_pointer){this = ref_pointer;}; }; //+------------------------------------------------------------------+ //| Construtor | //+------------------------------------------------------------------+ CTest::CTest() { } //+------------------------------------------------------------------+ //| Destrutor | //+------------------------------------------------------------------+ CTest::~CTest() { } CTest *objects[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { ArrayResize(objects,1,0); objects[0] = new CTest; objects[0].name = "Object_1"; objects[0].number = 1; //--- create timer EventSetMillisecondTimer(1); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- destroy timer EventKillTimer(); } //+------------------------------------------------------------------+ //| Timer function | //+------------------------------------------------------------------+ void OnTimer() { //--- int size = ArraySize(objects); //--- if(size < 10){ Print("Creating: " + IntegerToString(size)); ArrayResize(objects,size + 1,0); objects[size] = new CTest(); objects[size].Clone(objects[size - 1]); objects[size].number ++; objects[size].name = "Object_" + IntegerToString(objects[size].number); Print("Created: " + IntegerToString(size + 1)); Print(""); }else{ for(int i = 0; i < size;i++){ Print("Object: " + IntegerToString(i)); Print("---"); Print("Expected number: " + objects[i].name); Print("Real Number: " + IntegerToString(objects[i].number)); Print("---"); } for(int i=ArraySize(objects) - 1;i>=0;i--){ if(CheckPointer(objects[i]) != POINTER_INVALID) delete objects[i]; } ExpertRemove(); } } //+------------------------------------------------------------------+
I played around with your code (see attached).
Dubugging the objects[] array revealed that the attempted copy actually destroys the 2nd object, with the 2nd array location pointing to the original object (see pic)
I suggest you create your own get/set methods so you can pass the data to the 2nd object and manually clone the values.
Thanks, but the method is for not manually clone the values.
That's a really neat solution - I am glad to have learnt this!
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I this line code: objects[size] = new CTest();
create a new object but I need to clone the last object.
how to clone?
objects[size] = new CTest();