Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

assignment operator return value

40 views
Skip to first unread message

ruben safir

unread,
Dec 20, 2017, 8:02:17 AM12/20/17
to
In a typical syntax for an assignment operator, it might look like this:

T& operator=(const T& other) // copy assignment
{
if (this != &other) { // self-assignment check expected
if (other.size != size) { // storage cannot be reused
delete[] mArray; // destroy storage in this
size = 0;
mArray = nullptr; // preserve invariants in case
next line throws
mArray = new int[other.size]; // create storage in this
size = other.size;
}
std::copy(other.mArray, other.mArray + other.size, mArray);
}
return *this;
}


I have an example in my code that looks like this:

143 STATE & operator = (cost b)
144 {
145 r(b);
146 return *this;
147 }


So, I've forgotten what this is returning exactly.

A reference to a pointer to this is being being returned ?

what is that?

bitrex

unread,
Dec 20, 2017, 8:50:31 AM12/20/17
to
"this" is a pointer to the particular instance of the STATE class being
used for the assignment, *this is a _value_, and then that value is
returned from the operator overload by reference. Looks like it's being
used to modify some internal field of the class "in place" as in this
dummy example:

#include <iostream>

typedef double cost;

class State
{
public:
State(cost init_cost) : r_{init_cost}
{}

State& operator=(cost b)
{
r(b);
return *this;
}

operator double() const
{
return r_;
}

private:
cost r_;

void r(cost new_cost)
{
//First rule of government spending:
//Why have one when you can have two,
//at twice the price

r_ = new_cost*2;
}
};

int main()
{
auto my_state = State{5.0};
my_state = 6.0;
std::cout << my_state << std::endl;
}




Alf P. Steinbach

unread,
Dec 20, 2017, 10:11:26 AM12/20/17
to
On 12/20/2017 2:01 PM, ruben safir wrote:
> In a typical syntax for an assignment operator, it might look like this:
>
> T& operator=(const T& other) // copy assignment
> {
> if (this != &other) { // self-assignment check expected
> if (other.size != size) { // storage cannot be reused
> delete[] mArray; // destroy storage in this
> size = 0;
> mArray = nullptr; // preserve invariants in case
> next line throws
> mArray = new int[other.size]; // create storage in this
> size = other.size;
> }
> std::copy(other.mArray, other.mArray + other.size, mArray);
> }
> return *this;
> }

If the `new` expression throws then the current object is left in an
invalid state, with no buffer.

I.e. this code is not exception safe.

One way to fix it: do the allocation before the allocation, and bail out
of it fails (a simple way is to just let the exception propagate).


> I have an example in my code that looks like this:
>
> 143 STATE & operator = (cost b)
> 144 {
> 145 r(b);
> 146 return *this;
> 147 }

This looks like a specialized assignment operator, not a copy assignment.


> So, I've forgotten what this is returning exactly.

The code says it returns a `STATE&`.


> A reference to a pointer to this is being being returned ?

No, a reference to the object that `this` points to.


> what is that?

`this` points to the object that the member function is called on.


Cheers & hth.,

- Alf

James R. Kuyper

unread,
Dec 20, 2017, 11:16:35 AM12/20/17
to
On 12/20/2017 10:10 AM, Alf P. Steinbach wrote:
> ... do the allocation before the allocation ...

I think you meant to write something different.
0 new messages