Code:
#include <iostream>
using namespace std;
class Computer{
private :
char* op_name;
char* hardwarename;
public :
Computer();
Computer(const char* op_name,const char* hardwarename);
Computer(const Computer& anothercomputer);
Computer& operator=(const Computer& other);
void print();
};
Computer::Computer(){
op_name=NULL;
hardwarename=NULL;
};
Computer::Computer(const char* op_name,const char* hardwarename){
if(op_name!=NULL&&hardwarename!=NULL){
delete(this->op_name);
delete(this->hardwarename);
this->op_name=strdup(op_name);
this->hardwarename=strdup(hardwarename);
}
};
Computer::Computer(const Computer& anothercomputer){
if(op_name!=NULL&&hardwarename!=NULL){
delete(this->op_name);
delete(this->hardwarename);
this->op_name=strdup(anothercomputer.op_name);
this->hardwarename=strdup(anothercomputer.hardwarename);
}
};
Computer& Computer::operator=(const Computer& other){
if(op_name!=NULL&&hardwarename!=NULL){
delete(this->op_name);
delete(this->hardwarename);
this->op_name=strdup(other.op_name);
this->hardwarename=strdup(other.hardwarename);
}
return *this;
};
void Computer::print(){
cout<<"op_name : "<<op_name<<" hardwarename : "<<hardwarename<<endl;
}
int main(){
Computer serotik("Mac","Serotik");
serotik.print();
Computer enis=serotik;
enis.print();
}
Fehlermeldung
a.out(1025,0x7fff79a39000) malloc: *** error for object 0x7fff508cdc00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Serhads-MacBook-Pro

esktop Sero$ g++ computer.cpp
Serhads-MacBook-Pro

esktop Sero$ ./a.out
a.out(1030,0x7fff79a39000) malloc: *** error for object 0x7fff5b058c00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Serhads-MacBook-Pro

esktop Sero$