Operator = 抽象类 C++ OOP

问题描述 投票:0回答:0

我有班级员工

Class Employee{
protected:
 char* name
public:
 Employee();
 virtual ~Employee();
 float getSalary() = 0; // pure virtual funtion
 virtual Employee& operator = (const Employe& temp);
}

和派生类productEmployee

Class productEmployee:public Employee{
protected:
 int product;
public:
 productEmployee();
 virutal ~productEmployee();
 float getSalary();
 virtual productEmployee& operator = (const productEmployee& temp);
}

Operator= 用于两个类 雇员类

Employee& Employee::operator = (const Employee& temp) {
    if (this != &temp) {
        this->~Employee();
        name = new char[100]();
        strcpy_s(name, 100, temp.name);
    }
    return *this;
}

一流产品员工

productEmployee& productEmployee::operator = (const productEmployee& temp) {
    if (this != &temp) {
        //this->~productEmployee();
        Employee::operator=(temp);
        product = temp.product
    }
    return *this;
}

当我在 main 中创建时:

Employee *t1 = new productEmployee()
Employee *t2 = new productEmployee()
t1->input()...
*t2=*t1 //(t2=t1 is wrong, memory leak)

它不起作用,t1 和 t2 现在有相同的地址,我想要 t1!=t2 和 t1->name != t2->name(地址),我如何编辑 operator= 来得到它?

c++ oop inheritance operator-overloading abstract-class
© www.soinside.com 2019 - 2024. All rights reserved.