0

I have seen in some place an implementation of assignemt operator of class matrix looks like this:

class Matrix
{
private:

    int rows;
    int cols;
    int **mat;

public:

    Matrix& operator=(const Matrix& m)
        {
            if ( this != &m )
            {
                Matrix temp(m);
                std::swap(temp.rows, rows);
                std::swap(temp.cols, cols);
                std::swap(temp.mat, mat);
            }
            return *this;
        }
}

Now, suppose I want to use swap function for that matter , what is the alternative way to use it without writing std:swap? Am I suppose in such case to build a friend-function that implementing swap of two matrix?

ezra
  • 11
  • 2
  • 4
    This doesn't look right. It should be [copy and swap](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom), not pass by reference and swap. – NathanOliver Dec 18 '17 at 00:20
  • 2
    @NathanOliver: from a quick glance, this should be functionally equivalent, it still performs the copy but only after checking for self-assignment (not needed for correctness, but can be seen as a tiny extra optimization - or a pessimization of the common case). Still, it's more code (4 lines and two tokens extra) for no good reason. – Matteo Italia Dec 18 '17 at 00:36
  • 1
    @MatteoItalia It is not functionally equivalent. In copy and swap you steal the guts of a temporary copy. When you pass by reference it steals the guts of the object itself and in this case it actually wont work as it is `const`. – NathanOliver Dec 18 '17 at 00:39
  • 3
    @NathanOliver: it is still creating the temporary from which the guts are stolen, only inside the `if (this != &m)` block instead of having it implicitly created by receiving the argument by copy. – Matteo Italia Dec 18 '17 at 00:40
  • 1
    @MatteoItalia Ah, yep, missed that in the if block. – NathanOliver Dec 18 '17 at 00:41
  • 1
    Mind you, this is still worse than the usual copy & swap in case `Matrix` had a move constructor; in general, if you have a move constructor and a "copy and swap" assignment operator you get move assignment for free (as an rvalue reference can be used to move-construct the `operator=` argument, which is then swapped over the assignment target by `operator=`); here instead you'd have to implement move assignment on its own. – Matteo Italia Dec 18 '17 at 00:43
  • Read this: https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom – Jive Dadson Dec 18 '17 at 02:17
  • @MatteoItalia that is true for C++11 and later. Passing the input param by value allows efficient copying and moving. That was not the case prior to C++11, when passing the parameter by reference was preferred. – Remy Lebeau Dec 18 '17 at 04:59
  • @RemyLebeau: in the general case of course I agree, but even in C++03 the copy & swap idiom to implement the assignment operator was generally implemented using a by-value parameter for `operator=`, to leverage the implicit copy required to instantiate it. Of course that makes impossible to have a self-assignment elision optimization (the copy constructor is always invoked, unlike in OP variant), which, for a matrix class, may actually be desirable. – Matteo Italia Dec 18 '17 at 09:01

1 Answers1

0

Now, suppose I want to use swap function for that matter , what is the alternative way to use it without writing std:swap? Am I suppose in such case to build a friend-function that implementing swap of two matrix?

You have to swap the two Matrix objects. Whether you do it in the operator= function or another function is secondary.

As a matter of practice, I would recommend creating a function that swaps two Matrix objects and use it the implementation of the operator= function. You have various options for that.

  1. Make it a non-member friend function.
  2. Make it a static member function.
  3. Make it a non-static member function.

I would probably pick a static member function but I don't see any downsides to using one of the other two.

One thing to note is that you will end up using std::swap to swap the member variables of the Matrix objects in that function. It came across to me as though that is a concern for you.

R Sahu
  • 204,454
  • 14
  • 159
  • 270