In Interview, I asked to write constructor, Copy constructor and assignment operator. I wrote the following code.
He then asked me what is wrong in following code which I unable to answer, could you help me to know what is wrong?
Also, From question what interviewer was trying to find?
//constructor, copy constructor, assignment operator and destructor
class Employee
{
int id;
char *name;
public:
//constructor
Employee()
{
id=0;
*name = new char[];
}
//Copy constructor
Employee (const Employee& oldObj)
{
id = oldObj.id;
*name = *(oldObj.name);
}
//destructor
~Employee()
{
delete[] name;
}
//Assignment operator overloading
void operator = (const Employee& obj)
{
id = obj.id;
delete[] name;
*name = *(obj.name);
}
};
int main()
{
Employee a1;
Employee a2 = a1; //copy constructor
Employee a3;
a3 = a1;//assignment operator
}
what is wrong in following code
*name = pointer
is ill-formed. new char[]
is ill-formed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.