Declaring Template Class Variables - page 3

 
TraderTogami #:

I know you can do this, but say you did not know 20 was the max size of q and just want a general initialization 

Then assign q to be a Queue of 20 without adding a resize method (which is bad practice, since other generic classes don't have this workaround and resizing to 20 doesn't reset the variable). 



EDIT: Is "Queue<double>(20)" bad syntax? If so, what is the equivalent? 

Why all these complications ?

Just because you don't want to use a pointer ? Or because you don't want to add a Init() method ? Do you have a solid rationale for these decisions ?

Come on. Adjust your mind to the language and not the reverse.

 
if you don't want to work with pointer why are you using class after all? using a struct shall work as you wish.
 
Alain Verleyen #:

Why all these complications ?

Just because you don't want to use a pointer ? Or because you don't want to add a Init() method ? Do you have a solid rationale for these decisions ?

Come on. Adjust your mind to the language and not the reverse.

I think you are taking my question out of proportion. I have already stated that I try to avoid pointers if there is no memory-saving reason to. The only other reason to have pointers other than this is polymorphism. I don't hate memory-saving and polymorphism (haha), it is just that I don't think variable declaration I comes under these.


The reason why you should avoid pointers too is because if your program abruptly ends without the OnDeInit()'s you are left with a bunch of floating pointers leaking memory. "When will this happen." An Easy example is every time you run dll code you risk there being an error being in the dll code. Often when this happens the program ends without the OnDeInit() being able to run. 


My request was to ask if this code was even possible: 

 

Queue<double> q; 
// Work
q = Queue<double>(20); // Where 20 is the max size of the queue

Samuel Manoel De Souza #:
if you don't want to work with pointer why are you using class after all? using a struct shall work as you wish.

If it doesn't work, I will use a pointer or a struct. My question is "Is the pointer-less syntax I asked about possible or does an equivalent exist?" 

 
TraderTogami #:

I think you are taking my question out of proportion. I have already stated that I try to avoid pointers if there is no memory-saving reason to. The only other reason to have pointers other than this is polymorphism. I don't hate memory-saving and polymorphism (haha), it is just that I don't think variable declaration I comes under these.

Why out of proportion ? This is a forum, your topic will stay for the posterity and a lot of people will read it. That's why I think it's important to make things clear. You have a wrong view about pointers in MQL.

The reason why you should avoid pointers too is because if your program abruptly ends without the OnDeInit()'s you are left with a bunch of floating pointers leaking memory. "When will this happen." An Easy example is every time you run dll code you risk there being an error being in the dll code. Often when this happens the program ends without the OnDeInit() being able to run. 

We are talking about MQL not DLL There is not even real pointer in the language. There is no valid reason to not use a pointer in MQL. The only memory leak you can have will come from the platform, not from your code.

My request was to ask if this code was even possible: 

No it's not possible. You can't call a constructor when the object already exists, and the solutions proposed to have copy/assignment constructors are out of proportion with the initial issue. KISS.

 
Alain Verleyen #:

No it's not possible. You can't call a constructor when the object already exists, and the solutions proposed to have copy/assignment constructors are out of proportion with the initial issue. KISS.

Thanks this is what I wanted to know. 


Alain Verleyen #:
Why out of proportion ? This is a forum, your topic will stay for the posterity and a lot of people will read it. That's why I think it's important to make things clear. You have a wrong view about pointers in MQL.

We are talking about MQL not DLL There is not even real pointer in the language. There is no valid reason to not use a pointer in MQL. The only memory leak you can have will come from the platform, not from your code.

Interesting, so something like this won't cause a memory leak? 

class Basic_Class{
    public: 
        double basic_attribute;
        Basic_Class(double attr){this.basic_attribute = attr;}
        ~Basic_Class(){}
};

Basic_Class *p; 

p = new Basic_Class(2.0); 

p = new Basic_Class(3.0); 
// Work
delete p; 
 
TraderTogami #:

Thanks this is what I wanted to know. 


Interesting, so something like this won't cause a memory leak? 

In theory yes, so it's not good practice.

Practically no, because MQL4/MQL5 are managed languages. The platform will take care of it, you will get messages about it in the log and the platform will clean it up.

 
Alain Verleyen #:

In theory yes, so it's not good practice.

Practically no, because MQL4/MQL5 are managed languages. The platform will take care of it, you will get messages about it in the log and the platform will clean it up.

Thank you!

 
Alain Verleyen #:

In theory yes, so it's not good practice.

Practically no, because MQL4/MQL5 are managed languages. The platform will take care of it, you will get messages about it in the log and the platform will clean it up.

Arguably, you could say this applies to c/c++ as well, because windows or the OS destroys the process space. Still you will accumulate used memory over time, if you keep repeating this.

It's not a garbage collecting language like java. We, the coders do have to take care of such issues, and we should clean up, and not rely on the platform to do it for us. Especially when a software is running for prolonged time.


 
Dominik Christian Egert #:
Arguably, you could say this applies to c/c++ as well, because windows or the OS destroys the process space. Still you will accumulate used memory over time, if you keep repeating this.

It's not a garbage collecting language like java. We, the coders do have to take care of such issues, and we should clean up, and not rely on the platform to do it for us. Especially when a software is running for prolonged time.


^ Good Advice, Regardless of whether you are pointing to memory or controlling object descriptors, the delete operator exists for a reason.  

 
TraderTogami #: My request was to ask if this code was even possible: 
Queue<double> q; 
// Work
q = Queue<double>(20); // Where 20 is the max size of the queue

Queue<double> q(20);  // Where 20 is the max size of the queue
Reason: