c++ – My String Class

There are several bugs in your code. In the constructor that takes a C string, you compute the length of cstr and set pSize accordingly, but you allocate pCapacity bytes. You should allocate at least pSize bytes. I recommend you get rid of pCapacity and try to make the class work with just pSize first.
You make the same mistake in operator=().

Second, the way you compute the length is wrong. The compiler should have warned you about comparing cstr to "", if not turn on compiler warnings. The statement:

if(cstr == "")

Is comparing the pointers, not the contents of the C strings. The correct way is to write if(cstr(0) == ''), but even that is not necessary; you should just make the loop below work correctly even for the corner case of a zero length string.

It is very common to want to access a string object as a regular C string. And the member function data() almost allows you to do that, except that you don’t reserve memory for and don’t store a NUL byte at the end. I recommend you do that, it makes accessing a MyString as a C string much easier, and will prevent some accidents.

Since you were already using std::copy() to copy data around, consider using std::count_if() to compute the length of a C string without having to roll your own loop.

Calling delete on a NULL pointer is guaranteed to be safe. So:

    delete() pStr;

Or even better, as mentioned in the comments, use std::unique_ptr to handle memory for you. Don’t reinvent too many wheels at the same time.

Be aware that calling new might fail if the program has ran out of memory. In that case, an exception will be thrown. You can choose to ignore the exception, but you must do it in such a way that your object is left in a valid state. Consider that operator= might fail calling new, then pStr is left pointing to the old data which is already deleted. If the caller handles the exception, then you have a problem, since as soon as the destructor is called it tries to delete the memory again.

The solution is to try to allocate memory for the copy first before deleting the old memory:

if(pSize < rhs.size()){
    char *newStr = new char(rhs.size());
    delete() pStr;
    pStr = newStr;

pSize = rhs.size();