Konstruktoren und operator= mit der dynamischen Speicherreservierung

noob hier. Ich mache eine übung aus einem Buch und der compiler meldet keine Fehler, aber das Programm stürzt ab, wenn ich versuche, um Sie auszuführen.

Ich versuche zu laufen, ein kleines Programm für die Ausübung der verschiedenen Methoden für die Kuh-Klasse. Explizit hat: einen Konstruktor, einen Standard-Konstruktor, copy-Konstruktor, einen Destruktor, einen überladenen Zuweisungsoperator, und eine Methode, um seinen Inhalt anzuzeigen.

Ich werde das ganze Projekt:

Class Spezifikation:

//cow.h -- For project Exercise 12.1.cbp

class Cow
{
    char name[20]; //memory is allocated in the stack
    char *hobby;
    double weight;
public:
    Cow();
    Cow(const char *nm, const char *ho, double wt);
    Cow(const Cow &c);
    ~Cow();
    Cow & operator=(const Cow &c);
    void ShowCow() const; //display all cow data
};

Methoden Durchführung:

//cow.cpp -- Cow class methods implementation (compile with main.cpp)

#include <cstring>
#include <iostream>
#include "cow.h"

Cow::Cow() //default destructor
{
    strcpy(name, "empty");
    hobby = new char[6]; //makes it compatible with delete[]
    strcpy(hobby, "empty");
    weight = 0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{
    strcpy(name, nm); //name = nm; is wrong, it copies the address of the argument pointer (swallow copying)
    /*if (name[20] != '\0') //if it's not a string, make it a string (in case nm is larger than 20)
        name[20] = '\0';*/
    hobby = new char[strlen(ho) + 1]; //allocates the needed memory to hold the argument string
    strcpy(hobby, ho); //copies the pointed-to data from the argument pointer to the class pointer
    weight = wt;
}

Cow::Cow(const Cow &c) //copy constructor
{
    strcpy(name, c.name); //copies the value to the desired address
    char *temp = hobby; //stores the address of the memory previously allocated with new
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby); //copies the value to the new address
    delete[] temp; //deletes the previously new allocated memory
    weight = c.weight;
}

Cow::~Cow()
{
    delete[] hobby;
}

Cow & Cow::operator=(const Cow &c) //overloaded assignment operator
{
    strcpy(name, c.name);
    char *temp = hobby;
    hobby = new char[strlen(c.hobby) + 1];
    strcpy(hobby, c.hobby);
    delete[] temp;
    weight = c.weight;
    return *this;
}

void Cow::ShowCow() const
{
    std::cout << "Name: " << name << '\n';
    std::cout << "Hobby: " << hobby << '\n';
    std::cout << "Weight: " << weight << "\n\n";
}

Client:

//main.cpp -- Exercising the Cow class (compile with cow.cpp)

#include "cow.h"
#include <iostream>

int main()
{
    using std::cout;
    using std::cin;

    Cow subject1; //default constructor
    Cow subject2("Maria", "Reading", 120); //non-default constructor
    Cow subject3("Lula", "Cinema", 135);
    subject1 = subject3; //overloaded assignment operator
    Cow subject4 = subject2; //copy constructor
    subject1.ShowCow();
    subject2.ShowCow();
    subject3.ShowCow();
    subject4.ShowCow();

    cin.get();
    return 0;
}

War ich versteckt einige Teile des Codes zu suchen, die möglichen problem und es scheint das Programm nicht wie diese zwei Zeilen:

subject1 = subject3;
Cow subject4 = subject2

Und insbesondere in der überladene Zuweisungsoperator und copy-Konstruktor, wenn ich das ausblenden der delete[] temp Zeile, das Programm stürzt nicht ab.

Ich bin total noob und ist wahrscheinlich etwas dumm, aber ich kann nicht sehen, was ich falsch mache in diesen Definitionen.

Hilfe?

  • In C++, die Sie in der Regel nicht mit dem schreiben von Operatoren wie, dass, weil Sie sind so ähnlich wie copy-Konstruktoren und daher meist den doppelten code. Ich würde empfehlen, einen Blick auf die Copy und swap-idiom
  • vielen Dank für die Beratung! Jetzt bin ich nach einem Buch mit einer wirklich strukturierten Stück-für-Stück-Ansatz. Aber ich gebe es zu suchen.
InformationsquelleAutor Kurospidey | 2012-08-22
Schreibe einen Kommentar