Sorry if the title is wrong, I don't know how else to name it.
I have a class Name:
class Name {
char * name;
public:
Name(const char * n){
name = new char[strlen(n)+1];
strcpy(name,n);
}
Name& operator=(const Name& n){
name = new char[strlen(n.name)+1];
strcpy(name, n.name);
return *this;
}
Name(const Name& n){
*this = n;
}
};
And another class Person which should have Name object as it's member. How can I do this? I was thinking something like this:
class Person{
double height;
Name name;
public:
Person(double h, const Name& n){
height = h;
name = n;
}
};
But only this seems to work:
class Person{
double height;
Name * name;
public:
Person(double h, const Name & n){
height = h;
name = new Name(n);
}
};
Is it the right way to do it, and why can't I do it like I thought in the first place? Thanks