Jedes Mal, wenn eine der String Destructor getroffen wird es eine Breakpoint- Trigger, ich glaube, ich könnte die gleiche Variable zweimal oder nicht zuweisen die richtige Menge an Speicher während der Manipulation von m_str werden LöschenWindows-Trigger Breakpoint- auf Array Deletion
#include "String.h"
#include <iostream>
using namespace std;
String::String()
{
m_str = nullptr;
}
String::String(const char* newStr)
{
m_str = new char[strlen(newStr)+ 1];
strcpy(m_str, newStr);
}
String::~String()
{
if (m_str != nullptr)
{
delete[] m_str;
}
}
void String::operator=(const String & myString)
{
if (m_str != nullptr)
{
delete[] m_str; //Breakpoint Apears Here
};
m_str = new char[strlen(myString.m_str) + 1];
m_str = myString.m_str;
}
void String::operator=(char* newStr)
{
if (m_str != nullptr)
{
delete[] m_str;
};
m_str = new char[strlen(newStr) + 1];
m_str = newStr;
}
}
Und was ist Ihre Frage? –
Sie * müssen * einen Kopierkonstruktor haben, und für eine Ressource, die einen solchen Typ enthält, wäre ein Move-Konstruktor und ein Zuweisungsoperator sehr wünschenswert. (Move-Konstruktor: 'String (String && rhs): m_str (rhs.m_str) {rhs.m_str = nullptr;} '). –
check rule of three und rule of five –