What's the best way to overcome the following warning when working under the OOP paradigm: "structure have objects and cannot be copied"?

 
Hello there,

I'm modelling a graphic in a MQL5 struct like this:

struct bar
  {
   datetime time;
   // ...   
  };

struct graphic
  {
   CSymbolInfo symbol;
   ENUM_TIMEFRAMES timeframe;
   bar bars[];
  }; 

This is because later I want to define properties of type graphic in any class, for instance:

// ...
  
private:
      graphic           _Graphic;

// ...

That's classical OOP! Of course, my app conceives things in real-world terms through to the use of TADs.

The problem is due to the property bar of the structure graphic which is a dynamic array. This is why I get the error "structure have objects and cannot be copied" in the methods that use that graphic structre, for example:

graphic CSimpleRandomWinner::GetGraphic()
  { 
      return(_Graphic);
  }

This classical getter method fails when trying to return the private property graphic. I think that CSimpleRandomWinner would work in Java or C++, though I am not absolutely sure right now.

So my question is... What is the best way to work with MQL5 structs and classes that use dynamic arrays as properties?  I think that struct graphic is a good example!! Can MQL5 treat graphic in the way I am showing in this thread? Maybe there's something that I am missing..

Thanks a lot for your help in advance!

 

laplacianlab:

...

Documentation says :

Simple Structures

Structures that do not contain strings or objects of dynamic arrays are called simple structures; variables of such structures can be freely copied to each other, even if they are different structures. Variables of simple structures, as well as their array can be passed as parameters to functions imported from DLL.

Your structure isn't a simple structure so you can't use it as returned value of a function. You have to use parameter (passed by reference) or pointer.

 
angevoyageur:

Documentation says :

Your structure isn't a simple structure so you can't use it as returned value of a function. You have to use parameter (passed by reference) or pointer.

I understand now: "structure have objects and cannot be copied" The message is clear! I am going to definie a class hierarchy and relate classes to each other. Maybe this is one solution, I am also having a look to this solution: https://www.mql5.com/en/docs/basis/types/object_pointers

Thank you for clarifying this.   

Documentation on MQL5: Language Basics / Data Types / Object Pointers
Documentation on MQL5: Language Basics / Data Types / Object Pointers
  • www.mql5.com
Language Basics / Data Types / Object Pointers - Documentation on MQL5
 

Here it is the solution!

I have finally turned my graphic struct into a CGraphic class:

class CGraphic
  {
   public:
      string pair;
      CSymbolInfo symbol;
      ENUM_TIMEFRAMES timeframe;
      bar bars[];
  };

Then I can create any class with dynamic properties of type CGraphic inside:

class CSimpleRandomWinner
  {
   private:
      CGraphic*         _Graphic;
      // ...

   // Methods come here!

   CGraphic*         GetGraphic();

And init those properties of complex type inside the constructor:

CSimpleRandomWinner::CSimpleRandomWinner()
  {  
      _Graphic = new CGraphic();
  }

With all the above I can program my getter method like this:

CGraphic* CSimpleRandomWinner::GetGraphic()
  {  
      return(_Graphic);
  }

I have made some tests and I think everyhing is ok right now.

Thank you very much!

Create Your Own Trading Robot in 6 Steps!
Create Your Own Trading Robot in 6 Steps!
  • 2012.03.22
  • MetaQuotes Software Corp.
  • www.mql5.com
If you don't know how trade classes are constructed, and are scared of the words "Object Oriented Programming", then this article is for you. In fact, you do not need to know the details to write your own module of trading signals. Just follow some simple rules. All the rest will be done by the MQL5 Wizard, and you will get a ready-to-use trading robot!
 
laplacianlab:

Here it is the solution!

I have finally turned my graphic struct into a CGraphic class:

Then I can create any class with dynamic properties of type CGraphic inside:

And init those properties of complex type inside the constructor:

With all the above I can program my getter method like this:

I have made some tests and I think everyhing is ok right now.

Thank you very much!

Great. If you keep Graphic as a structure, that should work tool.