0

I have this:

//Constructor
ApplicationConstructor::ApplicationConstructor(string constructorCode, char *constructorName, string constructorEmail){
int i = strlen(constructorName);
ConstructorName = new char[i+1];
strncpy(ConstructorName, constructorName, (i+1));
ConstructorCode = constructorCode;
ConstructorEmail = constructorEmail;
}

//Copy constructor
ApplicationConstructor::ApplicationConstructor(const ApplicationConstructor &applicationConstructor){
int i = strlen(applicationConstructor.ConstructorName);
ConstructorName = new char[i+1];
strncpy(ConstructorName, applicationConstructor.ConstructorName, (i+1));
ConstructorCode = applicationConstructor.ConstructorCode;
ConstructorEmail = applicationConstructor.ConstructorEmail;   
}

ApplicationConstructor::~ApplicationConstructor(){
   cout << "Destruct the object ApplicationConstructor: " << this- 
   >ConstructorName << endl;
   delete[] this->ConstructorName;
} 

//Show the Application Constructor Data Method
void ApplicationConstructor::showData(){
   cout << " Code: " << this->ConstructorCode         
        << " Name: " << this->ConstructorName
        << " Email: " << this->ConstructorEmail
        << endl;
 } 

And this:

int main(int argc, char** argv) {    
ApplicationConstructor appConstructor1("3324",(char *)"Konstantinos Dimos", "konstantinos@uniwa.gr");
ApplicationConstructor appConstructor2("3332",(char *)"Maria Paulou", "nikos@uniwa.gr");

appConstructor2 = appConstructor1;
appConstructor2.showData();
}

And I get this when I run it:

 Code: 3324 Name: Konstantinos Dimos Email: konstantinos@uniwa.gr
 Destruct the object ApplicationConstructor: Konstantinos Dimos
 Destruct the object ApplicationConstructor: h�

What are these letters h�? I have made many times the same code on other programms but now I can't understand what is that? Any suggestion?

  • 1
    You use `std::string` (I assume) for `constructorCode` and `constructorEmail`, but not for `constructorName`. Why? If you did you wouldn't need an explicit copy-constructor or destructor. – Some programmer dude May 22 '20 at 10:57
  • Also note that `appConstructor2 = appConstructor1;` is an ***assignment*** not a copy-construction. – Some programmer dude May 22 '20 at 10:58
  • We need the code of destructor and the showData() function – Zig Razor May 22 '20 at 11:01
  • I add the destructor and the ShowData() method. I have to make it with pointers on ConstructorName property – Konstantinos May 22 '20 at 11:06
  • Look up the "rule of three". If you have to define one of a copy constructor, an assignment operator, or a destructor, it is necessary to define all three. You've shown definition for copy constructor, but not for the destructor (until your latest edit) and not for the assignment operator. Getting that wrong will explain your symptom. Or, better yet, look up "rule of zero" and, based on that, use `std::string` instead of managing dynamically allocated memory yourself. – Peter May 22 '20 at 11:07

1 Answers1

0

appConstructor2 = appConstructor1; is not calling the copy constructor, you end up with 2 objects pointing to the same allocated string, which I assume you then free in the deconstructor.

ApplicationConstructor appConstructor1("3332",(char *)"Maria Paulou", "nikos@uniwa.gr"); // calls constructor

ApplicationConstructor appConstructor3 = appConstructor1; // calls the copy

appConstructor3 = appConstructor1; // calls assignment constructor
lostbard
  • 5,065
  • 1
  • 15
  • 17