Should I avoid using dynamic objects?

 

Hi. I'm stuck between learning using dynamically created objects using the *new operator, and avoiding them. 

Is it possible to avoid them? 

 
Nelson Wanyama:

Hi. I'm stuck between learning using dynamically created objects using the *new operator, and avoiding them. 

Is it possible to avoid them? 

Don't use an object to say "I used an object" - nobody really gives a f*k about -  it's needed only if it eases your code 

 
  1.     Foo *foo2=new Foo("foo2");
    
    This creates an object on the heap and give you a handle to it. You are now responsible to delete it before the last handle goes out of scope.

  2. Foo foo2("foo2");
    This creates an object on the stack. It will be automatically deleted when it goes out of scope.


 
Nelson Wanyama:

Hi. I'm stuck between learning using dynamically created objects using the *new operator, and avoiding them. 

Is it possible to avoid them? 

We have to use them when it's needed, nothing to avoid using or want using.

It's all well explained here. Please note it's about C++ and not all what is said is applicable in MQL.

Why should I use a pointer rather than the object itself?
Why should I use a pointer rather than the object itself?
  • 2014.03.03
  • gEdringergEdringer 13.3k33 gold badges1111 silver badges2323 bronze badges
  • stackoverflow.com
I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for example this declaration: Or instead of using...
 
William Roeder:
  1. This creates an object on the heap and give you a handle to it. You are now responsible to delete it before the last handle goes out of scope.

  2. This creates an object on the stack. It will be automatically deleted when it goes out of scope.


Thanks. I have seen this answer somewhere in the forum. Do both have similar access methods or something changes in the way the codes should be formatted? 

Let me post what I'm trying to do. 

Say:

class CNet
{
        CNet(CArrayInt *&topology){}
        ~CNet(void){}
}
//---
void OnStart(){
        // I have this
        CArrayInt m_int;
        
        // Then I have another CNet that should take (CArrayInt *&topology). How will I put it?
        // Using your method, I have
        CNet m_net(___);        // I don't know how to insert a variable of type CArrayInt here.

        // I have tried
        CArrayInt m_int;
        CNet m_net(m_int);      // which does not compile
}
 
Alain Verleyen:

We have to use them when it's needed, nothing to avoid using or want using.

It's all well explained here. Please note it's about C++ and not all what is said is applicable in MQL.

Thank you Alain. I read the contents in the link and got motivated. Problem is, I'm new in this pointer/object/leaked memory subjects, having MQL5 as my first language. I can't seem to find materials that explain how they operate in MQL5, having started coding with the intention of creating Expert Advisers. 

I have come a long way I should say. And my recent project demanded that I know more about Object Pointers. 

I learn well by looking at code rather than reading do's and don'ts about coding. Yet MQL5 specific codes about these subjects are 'scarce'. I'm getting the hang of it though. 

I just need to know alternative workarounds to avoid dynamic objects. 

If you know of any approach that might help me get a hang of it more clearly, please share. 

 

Me: The rate at which I have to answer my own questions on this forum is worrying. 

Me: Anyway, knowing the use of dynamic objects is inevitable. You only have to be careful to ensure their deletion before they proceed out of scope. 

Me: Is there an alternative use that can meet the same requirements as a dynamic object? 

Forum: I don't know. Try reading stackoverflow forums.

Me: Do they include MQL in their discussions?

Forum: 

Me: Okay. Thanks. 😢

 
Nelson Wanyama:

Me: The rate at which I have to answer my own questions on this forum is worrying. 

Me: Anyway, knowing the use of dynamic objects is inevitable. You only have to be careful to ensure their deletion before they proceed out of scope. 

Me: Is there an alternative use that can meet the same requirements as a dynamic object? 

Forum: I don't know. Try reading stackoverflow forums.

Me: Do they include MQL in their discussions?

Forum: 

Me: Okay. Thanks. 😢

Your question has nothing specific to mql, it has been encountered by thousands of coders around the world in all languages that supports OOP and some kind of pointers.

I am suggesting you to not offense senior programmers here if you want to get answers to some of your questions.

Do your homework and stop relying on others to provide you the answers.

 
Alain Verleyen:

Your question has nothing specific to mql, it has been encountered by thousands of coders around the world in all languages that supports OOP and some kind of pointers.

I am suggesting you to not offense senior programmers here if you want to get answers to some of your questions.

Do your homework and stop relying on others to provide you the answers.

Alright. Point taken.

 
At this point you really need to complete a basic C++ OOP tutorial. You should fully understand how references and pointers work, and also how to properly utilize design patterns which safely manage memory, and how to debug it when you don't. I have yet to see a single MQL tutorial lay it all out there like you'd get with a proper C++ tutorial. You can ask questions until you think you got, then realize you don't, then have to go back to the drawing board. Trust me, save yourself the time and learn the fundamentals the right way. There are many tutorials out there but this is my preference: https://www.learncpp.com/cpp-tutorial/81-welcome-to-object-oriented-programming/
 
nicholi shen:
At this point you really need to complete a basic C++ OOP tutorial. You should fully understand how references and pointers work, and also how to properly utilize design patterns which safely manage memory, and how to debug it when you don't. I have yet to see a single MQL tutorial lay it all out there like you'd get with a proper C++ tutorial. You can ask questions until you think you got, then realize you don't, then have to go back to the drawing board. Trust me, save yourself the time and learn the fundamentals the right way. There are many tutorials out there but this is my preference: https://www.learncpp.com/cpp-tutorial/81-welcome-to-object-oriented-programming/

I figured it out myself. I don't see the point of going over to C++ when the minor details I'm after are exploited differently in cpp. 

Thanks though. 

I should write an article about pointers and dynamic objects for prospective learners. I'd hate to see someone get shun while trying to learn, like it happened to me.

Reason: