I have a very simple class with some char* members. Is strcat the best way to set their values?
class CImage {
public:
CImage() {};
char* Name;
};
void AddImage(const char* Name)
{
CImage* img = new CImage();
strcpy(img->Name, Name); // Is this correct or the best way?
}
Strongly advice you to drop the
char *and usestd::string.With
char *as member you need to bother about allocating(in constructor) and deallocating memory(in destructor) to the pointer yourself. Also, You need to manage the exceptions and do the manual management of the underlying data everytime you access it.Also you would have to ensure following Rule of Three for your class.
In short there are too many places where you can shoot yourself in the foot(or head) easily, so safest and best way is to use a
std::stringas member and save yourself all the trouble.