I am trying to create class Person with pointers to spouse(Person object) and table of children(Person objects). This class implements marriage(operator+=), divorce(method) and creating new children(operator++):
class Person{
private:
char* name;
int sex;
Person* spouse;
Person* children[5];
public:
Person();
Person(const Person&);
Person & operator =(const Person&);
Person & operator +=(Person&);//marriage
Person & operator ++();//new children
void divorce();//divorce
Person::~Person();
}
I created destructor which deletes children whenever there is no spouse:
Person::~Person(){
if (name !=NULL)
delete [] name;
name=NULL;
if (spouse!=NULL)
spouse->spouse =NULL;
else{
for (int i=0; i<5;i++){
if (children[i]!=NULL)
delete children[i];
children[i]=NULL;
}
}
}
I do not know if my copy constructor and operator= should create another instances of spouse and children. I tried to do this but I was stack in infinite reference. Is it possible to create a copy of Person object with properly assigned spouse and children?
Thanks in advance for any comments and suggestions Elkhunter