[Help with OOP] How to correctly resolve a static member? - page 2

 
Vladislav Boyko #:

By the fact that I only program in MQL😄

Regarding the constructor, everising is clear. My question was about this line (member resolving without assignment operator):

This is a rarely seen construction in MQL. To learn programming, I used only S. Kovalyov's book, the MQL documentation and articles on this site. And nothing but this.

I came up with something new for myself. But I did not find examples of using such a syntactic construct in MQL to validate this. That's why I asked you to confirm that the code is correct. And also because I'm paranoid and the fact that it works is not enough for me 😄

I see. It's correct and will not destroy your computer
 
Amir Yacoby #:
CheckPointer(GetPointer(a))

I don't usually need to get a pointer before checking it. I add newInstance() method to all classes that can be created with dynamic pointer (very often):

class CExample
  {
public:
   static bool newInstance(CExample *&a_pointer);
  };

static bool CExample::newInstance(CExample *&a_pointer)
  {
   ENUM_POINTER_TYPE pointerType = CheckPointer(a_pointer);
   if(pointerType == POINTER_DYNAMIC)
     {
      Print("CExample::newInstance() error: Object already created! Deleting...");
      delete a_pointer;
      pointerType = CheckPointer(a_pointer);
     }
   if(pointerType != POINTER_INVALID)
     {
      PrintFormat("CExample::newInstance() error: Invalid pointer type: %s. Line %i", EnumToString(pointerType), __LINE__);
      return(false);
     }
   a_pointer = new CExample;
   pointerType = CheckPointer(a_pointer);
   if(pointerType == POINTER_DYNAMIC)
      return(true);
   PrintFormat("CExample::newInstance() error: Invalid pointer type: %s. Line %i", EnumToString(pointerType), __LINE__);
   return(false);
  }

CExample *singleObj;

int OnInit()
  {
   if(!CExample::newInstance(singleObj))
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }

This is my paranoid wrapper for the 'new'. Perhaps I should use template polymorphism to create a single function that accepts any type of object. But I have not yet begun to do it this way, because usually not many classes are created dynamically. And such a single function would make it a little more difficult to split the code into files (adding one #include in each)

P.S. Just shared. Perhaps for someone it will be useful or someone will point out the shortcomings

 
Vladislav Boyko #:

I don't usually need to get a pointer before checking it. I add newInstance() method to all classes that can be created with dynamic pointer (very often):

This is my paranoid wrapper for the 'new'. Perhaps I should use template polymorphism to create a single function that accepts any type of object. But I have not yet begun to do it this way, because usually not many classes are created dynamically. And such a single function would make it a little more difficult to split the code into files (adding one #include in each)

P.S. Just shared. Perhaps for someone it will be useful or someone will point out the shortcomings

Not quite understand what you are trying to do.
What you just posted is not not a classic singleton (where one and only one instance is created by the create method) - like you previously posted. This time, you have a new instance each call, deleting the previous instance and creating a new one. 

Why do you need such thing and not a normal singleton with new instance?

 
Amir Yacoby #:

Not quite understand what you are trying to do.
What you just posted is not not a classic singleton (where one and only one instance is created by the create method) - like you previously posted. This time, you have a new instance each call, deleting the previous instance and creating a new one. 

Why do you need such thing and not a normal singleton with new instance?

To be honest, I don't know what a singleton is and can't fully answer your question😄

I saw in the documentation that the pointer should be checked before use. I have added this check. Just in case, I also added a check before creating an instance to make sure the pointer is invalid. I then wrapped it all up in a method and use it to create new instances (instead of using 'new').

My post with newInstance() is not related to my main question. In one of your posts you touched on getting and checking a pointer. And I developed this theme a little. Without any specific purpose.Actually, here I said about it:

Vladislav Boyko #:

P.S. Just shared. Perhaps for someone it will be useful or someone will point out the shortcomings

Thanks everyone for the replies! I found out what I wanted. Sorry if I caused confusion with my post about instantiation