C ++ Copy Constructor&运算符重载

时间:2017-03-27 00:10:31

标签: c++ class pointers constructor overloading

我是一个相对较新的类,并在上周介绍了复制构造函数和重载。我应该重载=运算符并使用它来使用类名分配多个变量。

出于某种原因,运行该程序会导致弹出窗口

  

program.cpp已停止响应。

我很肯定,由于我是C ++对象的新手,我缺少一些小的/重要的事情。

非常感谢任何建议!

#include<iostream>
#include<string>

using namespace std;

class Employee
{
private:
    char *name;
    string ID;
    double salary;
public:
    Employee() {}
    Employee(char *name, string eid, double salary) {}

    Employee(const Employee &obj)
    {
        name = new char;
        ID = obj.ID;
        salary = obj.salary;
    }

    ~Employee() {}

    void setName(char *n)
    {
        name = n;

    }

    void setID(string i)
    {
        ID = i;
    }

    void setSalary(double s)
    {
        salary = s;
    }

    char getName()
    {
        return *name;
    }

    string getID()
    {
        return ID;
    }

    double getSalary()
    {
        return salary;
    }

    Employee operator = (Employee &right)
    {
        delete[] name;
        ID = right.ID;
        salary = right.salary;
        return *this;
    }

};

int main()
{
    Employee e1("John", "e222", 60000), e2(e1), e3, e4;

    e3 = e4 = e2;

    e2.setName("Michael");
    e2.setSalary(75000);
    e3.setName("Aaron");
    e3.setSalary(63000);
    e4.setName("Peter");


    cout << "\nName: " << e1.getName() << "\nID: " << e1.getID() <<     "\nSalary: " << e1.getSalary() << endl;
    cout << "\nName: " << e2.getName() << "\nID: " << e2.getID() <<     "\nSalary: " << e2.getSalary() << endl;
    cout << "\nName: " << e3.getName() << "\nID: " << e3.getID() <<     "\nSalary: " << e3.getSalary() << endl;
    cout << "\nName: " << e4.getName() << "\nID: " << e4.getID() <<     "\nSalary: " << e4.getSalary() << endl;

    return 0;
}

3 个答案:

答案 0 :(得分:1)

此代码存在几个问题。

第一个问题出现在构造函数Employee(char *name, string eid, double salary) { const size_t bufferSize = strlen(name) + 1; this->name = new char[bufferSize]; memcpy(this->name, name, bufferSize); this->ID = eid; this->salary = salary; } 中,它只是无所事事并忽略传递的数据,而应该使用它来初始化字段(类成员数据)。

Employee(const Employee &obj)

第二个问题出现在复制构造函数name中,您只是在初始化Employee(const Employee &obj) { const size_t bufferSize = strlen(name) + 1; this->name = new char[bufferSize]; memcpy(this->name, name, bufferSize); ID = obj.ID; salary = obj.salary; } (使用char的单字节),就是这样。复制构造函数假设要做的是初始化类的字段(类成员),并将类对象的字段传递给它。

name

第三个问题是使用默认构造函数,它假设使用NULL初始化Employee() : name(NULL) {} ~Employee() { if (NULL != name) delete[] name; } 指针,以便析构函数可以很好地清理它:

name

第四个也是最后一个问题是赋值运算符,它假设正确初始化Employee operator = (Employee &right) { if (NULL != this->name) delete[] this->name; const size_t bufferSize = strlen(right.name) + 1; this->name = new char[bufferSize]; memcpy(this->name, right.name, bufferSize); ID = right.ID; salary = right.salary; return *this; } 成员数据而不是删除它(这没有意义)

{{1}}

答案 1 :(得分:0)

问题在于这一行:

        delete[] name;

您不应该首先删除尚未分配new的任何内容。如果删除上面的行,您的程序就可以了。 :)

答案 2 :(得分:-1)

以下是您的计划的略微修订版​​本:

#include<iostream>
#include<string>

using namespace std;

class Employee
{
private:
    char *name;
    string ID;
    double salary;
public:
    Employee() {}
    Employee(char *name, string eid, double salary)
        : name (name) // ADDED THESE
        , ID(eid)
        , salary(salary)
    {

    }

    Employee(const Employee &obj)
    {
        name = obj.name; // WAS: new char;
        ID = obj.ID;
        salary = obj.salary;
    }

    ~Employee() {}

    void setName(char *n)
    {
        name = n;
    }

    void setID(string i)
    {
        ID = i;
    }

    void setSalary(double s)
    {
        salary = s;
    }

    char * getName()
    {
        return name;
    }

    string getID()
    {
        return ID;
    }

    double getSalary()
    {
        return salary;
    }

    Employee operator = (const Employee &right)
    {
        name = right.name; // WAS: delete[] name;
        ID = right.ID;
        salary = right.salary;
        return *this;
    }

};

int main()
{
    Employee e1("John", "e222", 60000), e2(e1), e3, e4;

    e3 = e4 = e2;

    e2.setName("Michael");
    e2.setSalary(75000);
    e3.setName("Aaron");
    e3.setSalary(63000);
    e4.setName("Peter");


    cout << "\nName: " << e1.getName() << "\nID: " << e1.getID() << "\nSalary: " << e1.getSalary() << endl;
    cout << "\nName: " << e2.getName() << "\nID: " << e2.getID() << "\nSalary: " << e2.getSalary() << endl;
    cout << "\nName: " << e3.getName() << "\nID: " << e3.getID() << "\nSalary: " << e3.getSalary() << endl;
    cout << "\nName: " << e4.getName() << "\nID: " << e4.getID() << "\nSalary: " << e4.getSalary() << endl;

    return 0;
}