dgamma3
dgamma3

Reputation: 2371

returning a reference vs the type

I was looking over some code, in a textbook and they wrote:

Vector3 &operator =(const Vector3 &a) {
    x = a.x; y = a.y; z = a.z;
    return *this;
}

Does the following code produce the same, returning the type, not a reference to it(they both run):

Vector3 operator =(const Vector3 &a) {
    x = a.x; y = a.y; z = a.z;
    return *this;
}

my question: what is the difference between the two?

thanks daniel

Upvotes: 1

Views: 66

Answers (2)

Anonymous
Anonymous

Reputation: 2172

Vector3 b(1,2,3);
Vector3 a;

(a = b).x += 2.0;

Print(a.x);

If you use operator returning refernce, above code should print 3.0

In case of operator returning value it would print 1.0

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81976

Vector3 a, b;
(a = b).x = 3;

In this code, a.x should end up with the value of 3. In the second example you give, that won't happen.

Upvotes: 1

Related Questions