I`m writing string class by myself. And I dont know how to write Сopy constructor.
I have such code.
class S {
private:
char *string;
int l;
public:
S::S(const S &s){
string = new char[l+1];
memcpy(string,s.string,l+1);
}
};
Complier didn`t give any errors, but .exe closed by unknown error. I tried it to use in main() function.
S pop("Q6");
S str(pop);
So I`m looking forward to your help.
In your copy constructor
lhasn’t been initialized yet to to the length of the string, so it could be any value. You should initialize it (by copying the value froms.l) prior to using it.