2
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.

Dynamic
  • 1,553
  • 2
  • 19
  • 25

4 Answers4

2

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;
Dynamic
  • 1,553
  • 2
  • 19
  • 25
1

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

Mohammad Hossein Amri
  • 1,842
  • 2
  • 23
  • 43
0

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

Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20
0

you should NOT delete static objects - you will get "delete invalid pointer"-message from compiler in print area... therefore the check should be done as Daniel Kniaz answered... but as so as you are creating the object inside the wrapper - you'd better have a check for deletion & delete it in the Destructor of its wrapper (though I doubt, that you really should use here another class for CPerson creation - yuo can create its object in its - CPerson's - Constructor)

JeeyCi
  • 354
  • 2
  • 9