简体   繁体   English

了解运算符重载的效用

[英]Understanding utility of an operator overloading

I've been working on the code of a class "DenseMatrix" that aims to create regular matrices. 我一直在研究旨在创建常规矩阵的“ DenseMatrix”类的代码。

Going through the code, there are 2 or 3 things I can't quite understand. 在代码中,有2到3件事我不太了解。
So first here's the code of this class : 所以首先这是此类的代码:

#include<iostream>
#include<complex>
#include<vector>
#include <cassert>

using namespace std ;

class DenseMatrix{
    typedef complex<double> Cplx;

private:
    int nr, nc;
    vector<Cplx> data;

public :
    DenseMatrix(const int& nr0, const int& nc0){
    nr = nr0; nc = nc0; data.resize(nr*nc,0);}

    DenseMatrix(const DenseMatrix& M){
    nr = M.nr; nc = M.nc; data.resize((M.data).size());
    for (int j=0; j<data.size(); j++) {data[j]=M.data[j];} }

    void operator=(const DenseMatrix& M){
    nr = M.nr ; nc = M.nc ; data.resize((M.data).size());
    for (int j=0; j<data.size() ; j++){data[j]=M.data[j];} }

    Cplx& operator () (const int& j ,const int& k) {
    assert(0<=j && j<nr && 0<=k && k<nc) ; return data[k+j*nc];}

    const Cplx& operator () (const int& j ,const int& k) const {
    assert(0<=j && j<nr && 0<=k && k<nc) ; return data[k+j*nc];}

    friend ostream& operator<<(ostream& o , const DenseMatrix& M){
    for ( int j =0; j<nr ; j++){ for ( int k=0; k<nc; k++){o << M(j,k) << "\ t " ;} o << endl ;}
    //return o ;}
};

First thing is what's the utility of defining the "=" operator if we can actually use the copy constructor and obtain the same result ? 首先,如果我们实际上可以使用复制构造函数并获得相同的结果,那么定义“ =”运算符的用途是什么?

Second thing, if my understanding is right, Cplx& operator () will return a reference and this reference will actually allow us to modify a private attribute (an element of the matix). 第二件事,如果我的理解正确, Cplx& operator ()将返回一个引用,并且该引用实际上将使我们能够修改private属性(matix的元素)。 But what does the second definition of operator, const Cplx& operator () (const int& j ,const int& k) const , does ? 但是运算符的第二个定义const Cplx& operator () (const int& j ,const int& k) const是什么呢? What's its utility ? 它的作用是什么?

Thanks ! 谢谢 !

First thing is what's the utility of defining the "=" operator if we can actually use the copy constructor and obtain the same result ? 首先,如果我们实际上可以使用复制构造函数并获得相同的结果,那么定义“ =”运算符的用途是什么?

Because you can't use the copy constructor to obtain the same result. 因为您不能使用复制构造函数来获得相同的结果。 The copy constructor is a constructor; 复制构造函数是一个构造函数; it gets used when you construct a matrix. 在构造矩阵时会使用它。 The assignment operator lets you assign to an already constructed matrix. 赋值运算符使您可以分配给已经构造的矩阵。

Second thing, if my understanding is right, Cplx& operator () will return a reference and this reference will actually allow us to modify [an element of the matrix]. 第二件事,如果我的理解正确,那么Cplx&operator()将返回一个引用,并且该引用实际上将使我们能够修改[矩阵的元素]。

But what does the second definition of operator, const Cplx& operator () (const int& j ,const int& k) const , do? 但是运算符的第二个定义const Cplx& operator () (const int& j ,const int& k) const是做什么的?

That version is for when you have a const DenseMatrix . 该版本适用于具有const DenseMatrix Imagine that you used the normal operator() - you'd get a Cplx& which you could use to change the matrix elements. 想象一下,您使用了普通的operator() -您将获得一个Cplx& ,可用于更改矩阵元素。 But you aren't allowed to change the matrix elements if it's a const matrix. 但是,如果它是const矩阵,则不允许更改矩阵元素。 The compiler won't allow the first version to be used on a const matrix. 编译器不允许在const矩阵上使用第一个版本。

The last const (before the { ) is saying that this function is okay to call on a const DenseMatrix . 最后一个const (在{之前)表示该函数可以调用const DenseMatrix

First thing is what's the utility of defining the "=" operator if we can actually use the copy constructor and obtain the same result ? 首先,如果我们实际上可以使用复制构造函数并获得相同的结果,那么定义“ =”运算符的用途是什么?

They are two different beasts. 他们是两种不同的野兽。 The copy constructor allows you to create a new object as a copy of an existing one; 复制构造函数允许您创建一个新对象作为现有对象的副本; the assignment operator allows you to copy an object over an existing one. 赋值运算符使您可以在现有对象上复制对象。 So: 所以:

DenseMatrix foo;
...
DenseMatrix bar(foo); // copy constructor
...
foo = bar; // assignment operator

The difference is subtle but important: the copy constructor starts with a pristine object, while the assignment operator generally has also to get rid of the existing data. 区别是微妙的但很重要:复制构造函数以原始对象开头,而赋值运算符通常还必须摆脱现有数据。

Still, given that the assignment operator generally is very similar to a destructor + copy constructor (with most code being duplicated), often the copy & swap idiom is used to minimize code duplication (while achieving other useful properties - such as strong exception guarantees in the process). 不过,由于赋值运算符通常与析构函数+复制构造函数非常相似(大多数代码都重复了),因此通常使用复制和交换惯用法来最大程度地减少代码重复(同时实现其他有用的属性-例如,强大的异常保证)该过程)。


But what does the second definition of operator, const Cplx& operator () (const int& j ,const int& k) const , does ? 但是运算符的第二个定义const Cplx& operator () (const int& j ,const int& k) const是什么呢? What's its utility ? 它的作用是什么?

The const overload is the one that gets invoked over const instances of the class (or to "regular" instances accessed through a const pointer or reference); const重载是通过类的const实例(或通过const指针或引用访问的“常规”实例)调用的; in this case, they will return the same data as the non- const version, but as a const reference instead of a plain reference, thus not allowing the caller to modify the data of the matrix, as per the constness of the object over which is it invoked. 在这种情况下,它们将返回与非const版本相同的数据,但作为const引用而不是普通引用,因此不允许调用者根据对象的const修改矩阵的数据。它被调用了吗?

References don't allow access to private attributes. 引用不允许访问私有属性。 It's no different than working with the bare object itself. 这与处理裸对象本身没有什么不同。

The idiom of having two versions of a function, one const and one not, is quite common. 具有两个版本的函数(一个const和一个非const的习惯用法很常见。 Since the function is returning a reference, it returns a const reference from the const function. 由于函数正在返回引用,因此它从const函数返回const引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM