How to return an object or struct from a method in MQL?











up vote
0
down vote

favorite












struct Person {         
string FirstName;
string LastName;
};

class Builder {
public:
Person Builder::Build() {
Person person;
person.FirstName = "FirstName";
person.LastName = "LastName";
return person;
};
};


When I compile this it gives me the below error:



'return' - structure have objects and cannot be copied.



I just need to create a struct or class object and return it, I don't want to do any copying.



I tried using & and * combinations but didn't work. I tried with a class instead of struct and it didn't work either.



I also tried with class as shown below:



class Person {   
public:
string FirstName;
string LastName;
};

class Builder {
public:
Person* Build() {
Person person;
person.FirstName = "FirstName";
person.LastName = "LastName";
return &person;
};
};
int OnInit()
{

Builder builder;
Person* person = builder.Build();
string firstName = person.FirstName;
return(INIT_SUCCEEDED);
}


And it gives me invalid pointer access when accessing person.FirstName in the OnInit() method at runtime.










share|improve this question




























    up vote
    0
    down vote

    favorite












    struct Person {         
    string FirstName;
    string LastName;
    };

    class Builder {
    public:
    Person Builder::Build() {
    Person person;
    person.FirstName = "FirstName";
    person.LastName = "LastName";
    return person;
    };
    };


    When I compile this it gives me the below error:



    'return' - structure have objects and cannot be copied.



    I just need to create a struct or class object and return it, I don't want to do any copying.



    I tried using & and * combinations but didn't work. I tried with a class instead of struct and it didn't work either.



    I also tried with class as shown below:



    class Person {   
    public:
    string FirstName;
    string LastName;
    };

    class Builder {
    public:
    Person* Build() {
    Person person;
    person.FirstName = "FirstName";
    person.LastName = "LastName";
    return &person;
    };
    };
    int OnInit()
    {

    Builder builder;
    Person* person = builder.Build();
    string firstName = person.FirstName;
    return(INIT_SUCCEEDED);
    }


    And it gives me invalid pointer access when accessing person.FirstName in the OnInit() method at runtime.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      struct Person {         
      string FirstName;
      string LastName;
      };

      class Builder {
      public:
      Person Builder::Build() {
      Person person;
      person.FirstName = "FirstName";
      person.LastName = "LastName";
      return person;
      };
      };


      When I compile this it gives me the below error:



      'return' - structure have objects and cannot be copied.



      I just need to create a struct or class object and return it, I don't want to do any copying.



      I tried using & and * combinations but didn't work. I tried with a class instead of struct and it didn't work either.



      I also tried with class as shown below:



      class Person {   
      public:
      string FirstName;
      string LastName;
      };

      class Builder {
      public:
      Person* Build() {
      Person person;
      person.FirstName = "FirstName";
      person.LastName = "LastName";
      return &person;
      };
      };
      int OnInit()
      {

      Builder builder;
      Person* person = builder.Build();
      string firstName = person.FirstName;
      return(INIT_SUCCEEDED);
      }


      And it gives me invalid pointer access when accessing person.FirstName in the OnInit() method at runtime.










      share|improve this question















      struct Person {         
      string FirstName;
      string LastName;
      };

      class Builder {
      public:
      Person Builder::Build() {
      Person person;
      person.FirstName = "FirstName";
      person.LastName = "LastName";
      return person;
      };
      };


      When I compile this it gives me the below error:



      'return' - structure have objects and cannot be copied.



      I just need to create a struct or class object and return it, I don't want to do any copying.



      I tried using & and * combinations but didn't work. I tried with a class instead of struct and it didn't work either.



      I also tried with class as shown below:



      class Person {   
      public:
      string FirstName;
      string LastName;
      };

      class Builder {
      public:
      Person* Build() {
      Person person;
      person.FirstName = "FirstName";
      person.LastName = "LastName";
      return &person;
      };
      };
      int OnInit()
      {

      Builder builder;
      Person* person = builder.Build();
      string firstName = person.FirstName;
      return(INIT_SUCCEEDED);
      }


      And it gives me invalid pointer access when accessing person.FirstName in the OnInit() method at runtime.







      class pointers struct mql4






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 12:47

























      asked Nov 10 at 12:38









      Dynamic

      75311019




      75311019
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?



          class cPerson {   
          public:
          string FirstName;
          string LastName;
          };

          class cBuilder {
          public:
          cPerson* Build() {
          cPerson* person = new cPerson();
          person.FirstName = "firstname";
          return person;
          };
          };
          cBuilder builder;
          cPerson* person = builder.Build();
          string age = person.FirstName;





          share|improve this answer




























            up vote
            0
            down vote













            You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);



            Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.



            Regarding the initial question - you cannot have string inside a struct, only primitives






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239038%2fhow-to-return-an-object-or-struct-from-a-method-in-mql%23new-answer', 'question_page');
              }
              );

              Post as a guest
































              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?



              class cPerson {   
              public:
              string FirstName;
              string LastName;
              };

              class cBuilder {
              public:
              cPerson* Build() {
              cPerson* person = new cPerson();
              person.FirstName = "firstname";
              return person;
              };
              };
              cBuilder builder;
              cPerson* person = builder.Build();
              string age = person.FirstName;





              share|improve this answer

























                up vote
                0
                down vote













                Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?



                class cPerson {   
                public:
                string FirstName;
                string LastName;
                };

                class cBuilder {
                public:
                cPerson* Build() {
                cPerson* person = new cPerson();
                person.FirstName = "firstname";
                return person;
                };
                };
                cBuilder builder;
                cPerson* person = builder.Build();
                string age = person.FirstName;





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?



                  class cPerson {   
                  public:
                  string FirstName;
                  string LastName;
                  };

                  class cBuilder {
                  public:
                  cPerson* Build() {
                  cPerson* person = new cPerson();
                  person.FirstName = "firstname";
                  return person;
                  };
                  };
                  cBuilder builder;
                  cPerson* person = builder.Build();
                  string age = person.FirstName;





                  share|improve this answer












                  Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?



                  class cPerson {   
                  public:
                  string FirstName;
                  string LastName;
                  };

                  class cBuilder {
                  public:
                  cPerson* Build() {
                  cPerson* person = new cPerson();
                  person.FirstName = "firstname";
                  return person;
                  };
                  };
                  cBuilder builder;
                  cPerson* person = builder.Build();
                  string age = person.FirstName;






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 13:10









                  Dynamic

                  75311019




                  75311019
























                      up vote
                      0
                      down vote













                      You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);



                      Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.



                      Regarding the initial question - you cannot have string inside a struct, only primitives






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);



                        Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.



                        Regarding the initial question - you cannot have string inside a struct, only primitives






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);



                          Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.



                          Regarding the initial question - you cannot have string inside a struct, only primitives






                          share|improve this answer












                          You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);



                          Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.



                          Regarding the initial question - you cannot have string inside a struct, only primitives







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          Daniel Kniaz

                          2,2102715




                          2,2102715






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239038%2fhow-to-return-an-object-or-struct-from-a-method-in-mql%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest




















































































                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Error while running script in elastic search , gateway timeout

                              Adding quotations to stringified JSON object values