mardi 28 juillet 2015

C++ crash when summarazing class instances

It's a simple Hello World code, which should use copy constructor to summarize objects

below is a code and output it generates

i guess the crash is because destructor called where it shouldn't (or wasn't expected by author of my C++ learning book lol), but may be you could give me few advices

i use default GNU GCC compiler of Code Blocks with extra options -std=c++11 -fno-elide-constructors (they dont matter in this case anyway)

#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;
}

the output is

Constructor called for: Hello
Constructor called for: World
Constructor called for:  of C++
Constructor called for: overwrite this
operator+ called for 'Hello ' to add: World
Default constructor called
Destructor called for: Hello World
operator+ called for '├РРРРР■   ' to add:  of C++
Default constructor called
Destructor called for: ├РРРРР■    of C++
Move assignment operator for 'overwrite this' to move from: 
<CRASH>
Process returned -1073741819 (0xC0000005)   execution time : 37.566 s
Press any key to continue.

Aucun commentaire:

Enregistrer un commentaire