Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 864
Dynamic Avatar asked Oct 12 '25 05:10

Dynamic


2 Answers

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;
like image 52
Dynamic Avatar answered Oct 16 '25 05:10

Dynamic


First you need a default constructor and copy constructor in your code. second you need to initialize the struct to default value, the struct only allocate space in the memory and not initialize it, so you can end up with cases where a long variable has some weird value like -1823834393939, so always set your struct to default values.

then when you return a class or struct from a function. the copy constructor will be called that copy the values. so if you don't want to return the exact object you've created in your class you don't need to return a reference

struct Person
{
    string FirstName;
    string LastName;
    Person()
    {
        FirstName = "";
        LastName = "";
    }
    Person(Person &that)
    {
        FirstName = that.FirstName;
        LastName = that.LastName;
    }
};

class Builder
{
public:
    Person Builder::Build(string argFirstname, string argLastName)
    {
        Person person;
        person.FirstName = argFirstname;
        person.LastName = argLastName;
        return person;
    };
};

void OnStart()
{
    Builder b;
    Person p = b.Build("Mohammad Hossein", "amri");
    Print(p.FirstName + " " + p.LastName);
}

the output will be

enter image description here

like image 44
Mohammad Hossein Amri Avatar answered Oct 16 '25 05:10

Mohammad Hossein Amri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!