0

我已经为任意模型实现了一个类,如下所示

class model_lincommands : public QAbstractTableModel
{
    Q_OBJECT

    ...

private:
    QList<lin_display_role> datalist_display_roles_;
    QList<LIN_FRAME> datalist_frames_;
    QList<LIN_CMD> datalist_commands_;
};

此外,我有一个自定义结构/类,称为LIN_FRAME,它是单独定义的。

我现在的目标是依靠类型转换来重载-operator 并为datalist_frames_=实现一个方便的复制功能,它来自 type 。QList<LIN_FRAME>

在中model_lincommands,我尝试了以下方法:

inline QList<LIN_FRAME> operator= (QList<LIN_FRAME> const& rhs)
{
    return this->datalist_frames_;
}

然后通过调用它

QList<LIN_FRAME> tframe = *model;

而是*model指向实例化model_lincommands类的指针。

但是,这根本不起作用。你能帮我看看这里有什么问题吗?

4

2 回答 2

2

您的运算符重载不正确。尝试datalist_frames_ = rhs; return *this;而不是return this->datalist_frames_; 编辑

于 2013-09-24T10:21:19.113 回答
1

看看这个:

// Operator overloading in C++
//assignment operator overloading
#include<iostream>
using namespace std;

class Employee
{
private:
int idNum;
double salary;
public:
Employee ( ) {
    idNum = 0, salary = 0.0;
}

void setValues (int a, int b);
void operator= (Employee &emp );

};

void Employee::setValues ( int idN , int sal )
{

salary = sal; idNum = idN;

}

void Employee::operator = (Employee &emp)  // Assignment operator overloading function
{
salary = emp.salary;
}

int main ( )
{

Employee emp1;
emp1.setValues(10,33);
Employee emp2;
emp2 = emp1;        // emp2 is calling object using assignment operator

}

您可以根据需要在类中定义运算符函数...

于 2013-09-27T16:37:35.580 回答