c++在总结类实例时崩溃

C++ crash when summarazing class instances

本文关键字:实例 崩溃 c++      更新时间:2023-10-16

这是一个简单的Hello World代码,应该使用复制构造函数来总结对象

下面的

是一个代码,输出它生成

我猜崩溃是因为析构函数调用了它不应该调用的地方(或者我的c++学习书的作者没有预料到),但也许你可以给我一些建议

我使用默认的GNU GCC编译器的代码块和额外的选项-std=c++11 -fno-elide-constructors(它们在这种情况下不管怎样)

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
class MyString
{
private:
  char* Buffer;
  MyString(): Buffer(NULL)
  {
    cout << "Default constructor called" << endl;
  }
public:
  MyString( const char* InitialInput )
  {
    cout << "Constructor called for: " << InitialInput << endl;
    if(InitialInput != NULL)
    {
      Buffer = new char [strlen(InitialInput)+1];
      strcpy( Buffer, InitialInput );
    }
    else
      Buffer = NULL;
  }
  MyString operator+ (const MyString& AddThis)
  {
    cout << "operator+ called for '" << Buffer << "' to add: " << AddThis.Buffer << endl;
    MyString NewString;
    if (AddThis.Buffer != NULL)
    {
      NewString.Buffer = new char[GetLenght() + strlen( AddThis.Buffer ) + 1];
      strcpy( NewString.Buffer, Buffer );
      strcat( NewString.Buffer, AddThis.Buffer );
    }
  }
  MyString& operator= (const MyString& CopySource)
  {
    cout << "Copy assignment operator for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;
    if ((this != &CopySource) && (CopySource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      // гарантирует глубокую копию с предварительным
      // резервированием собственного буфера
      Buffer = new char [strlen(CopySource.Buffer) + 1];
      // копирование оригинала в локальный буфер
      strcpy(Buffer, CopySource.Buffer);
    }
    return *this;
  }
  MyString( const MyString& CopySource )
  {
    cout << "Copy constructor for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;
    if(CopySource.Buffer != NULL)
    {
      Buffer = new char [strlen(CopySource.Buffer)+1];
      strcpy(Buffer,CopySource.Buffer);
    }
    else
      Buffer = NULL;
  }
  ~MyString()
  {
    cout << "Destructor called for: " << Buffer << endl;
    if( Buffer != NULL )
      delete [] Buffer;
  }
  int GetLenght()
  {
    return strlen(Buffer);
  }
  operator const char*()
  {
    return Buffer;
  }
};
int main( )
{
  MyString Hello("Hello ");
  MyString World("World");
  MyString CPP(" of C++");
  MyString sayHelloAgain ("overwrite this");
  sayHelloAgain = Hello + World + CPP;
  return 0;
}

输出为

构造函数被调用:Hello
为:World
调用的构造函数调用c++的构造函数
调用的构造函数:overwrite this
操作符+要求'Hello '添加:World
默认构造函数名为
调用的析构函数:Hello World
操作符+调用'├РРРРР■'来添加:of c++
默认构造函数名为
为:├РРРРР■of c++
调用析构函数将'overwrite this'的赋值操作符从
移动到
事故
1073741819 (0xC0000005)执行时间:37.566 s
按任意键继续。

您的错误在您的copy constructor

cout << "Copy constructor for '" << Buffer << "' to copy from: " << CopySource.Buffer << endl;

当对象在缓冲区中为NULL时,您正在尝试打印buffer

谢谢大家如果有人感兴趣,下面是固定的代码(在copy上添加了move构造函数)

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
class MyString
{
private:
  char* Buffer;
  MyString(): Buffer(NULL)
  {
    cout << "Default constructor called" << endl;
  }
public:
  MyString( const char* InitialInput )
  {
    cout << "Constructor called for: " << InitialInput << endl;
    if(InitialInput != NULL)
    {
      Buffer = new char [strlen(InitialInput)+1];
      strcpy( Buffer, InitialInput );
    }
    else
      Buffer = NULL;
  }
  MyString operator+ (const MyString& AddThis)
  {
    cout << "operator+ called: " << AddThis.Buffer << endl;
    MyString NewString;
    if (AddThis.Buffer != NULL)
    {
      NewString.Buffer = new char[GetLenght() + strlen( AddThis.Buffer ) + 1];
      strcpy( NewString.Buffer, Buffer );
      strcat( NewString.Buffer, AddThis.Buffer );
    }
    return NewString;
  }
  MyString& operator= (const MyString& CopySource)
  {
    cout << "Copy assignment operator to copy from: " << CopySource.Buffer << endl;
    if ((this != &CopySource) && (CopySource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      Buffer = new char [strlen(CopySource.Buffer) + 1];
      strcpy(Buffer, CopySource.Buffer);
    }
    return *this;
  }
  MyString( const MyString& CopySource )
  {
    cout << "Copy constructor to copy from: " << CopySource.Buffer << endl;
    if(CopySource.Buffer != NULL)
    {
      Buffer = new char [strlen(CopySource.Buffer)+1];
      strcpy(Buffer,CopySource.Buffer);
    }
    else
      Buffer = NULL;
  }
  MyString( MyString&& MoveSource)
  {
    cout << "Move constructor to move from: " << MoveSource.Buffer << endl;
    if(MoveSource.Buffer != NULL)
    {
      Buffer = MoveSource.Buffer;
      MoveSource.Buffer = NULL;
    }
  }
  MyString& operator= (MyString&& MoveSource)
  {
    cout << "Move assignment operator to move from: " << MoveSource.Buffer << endl;
    if ((this != &MoveSource) && (MoveSource.Buffer != NULL))
    {
      if (Buffer != NULL)
        delete[ ] Buffer;
      Buffer = new char [strlen(MoveSource.Buffer) + 1];
      strcpy(Buffer, MoveSource.Buffer);
    }
    return *this;
  }
  ~MyString()
  {
    if( Buffer != NULL )
      delete [] Buffer;
  }
  int GetLenght()
  {
    return strlen(Buffer);
  }
  operator const char*()
  {
    return Buffer;
  }
};
int main( )
{
  MyString Hello("Hello ");
  MyString World("World");
  MyString CPP(" of C++");
  MyString sayHelloAgain ("overwrite this");
  sayHelloAgain = Hello + World + CPP;
  return 0;
}

构造函数被调用:Hello
为:World
调用的构造函数调用c++的构造函数
调用的构造函数:overwrite this
操作符+被叫:World
默认构造函数名为
移动构造函数:Hello World
operator+ called: of c++
默认构造函数名为
移动构造函数从:c++的Hello World