i am trying to implement and + operator that gets rhs obj and lhs obj, the object contains char* string; for for e.g s1 contains "eden" s2 contains "sh" I want that s3 = s1 +s2 will be "eden sh" yet I cannot figure it out. I don't USE vector or std::string because the assignment was to do char* arr code:
auto *buff = new char[std::strlen(lhs.getName()) + std::strlen(rhs.getName()) + 2];
assert(buff);
std::strncpy(buff, lhs.getName(), std::strlen(lhs.getName()));
std::strncpy(buff + std::strlen(lhs.getName())," ", sizeof(char));
std::strncpy(buff + std::strlen(lhs.getName()), rhs.getName(), std::strlen(rhs.getName()));
tmp.setName(buff);
***set name is a function that copies buffer to m_name private data member.
the result is edensh
If you compare these two records
you will see that they both start write at position
buff + std::strlen(lhs.getName()). So the second call ofstrncpyoverwrites symbols of the previous call ofstrncpy.Also there is another problem. The result array does not contain a string because the stored sequence of characters is not appended with the terminating zero character
'\0'.Instead you could write for example
Also instead of this call of the function
strcpyit is enough to write
So you can also write