why is this "not assignable? "

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1288 Views - Last Post: 15 August 2012 - 12:30 PM Rate Topic: -----

#16 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 09:47 AM

Writing cout << a += b is the same as writing the following:

cout << a;
cout += b;



Do you understand why cout += b can not work?
Was This Post Helpful? 0
  • +
  • -

#17 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 10:15 AM

View Postsepp2k, on 15 August 2012 - 09:47 AM, said:

Writing cout << a += b is the same as writing the following:

cout << a;
cout += b;



Do you understand why cout += b can not work?


I understand why

cout += b;


can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?

and what about this example from wiki?
Vector2D& Vector2D::operator+=(const Vector2D& right)
{
    this->x += right.x;
    this->y += right.y;
    return *this;
}


This post has been edited by healix: 15 August 2012 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#18 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 10:26 AM

View Posthealix, on 15 August 2012 - 07:15 PM, said:

I understand why

cout += b;


can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?


It doesn't. The fix here isn't to make += work with a stream as the left operand, it's to re-write the expression, so that the left operand of += is a and not cout << a.

Quote

and what about this example from wiki?


What about it?
Was This Post Helpful? 1
  • +
  • -

#19 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1890
  • View blog
  • Posts: 5,680
  • Joined: 05-May 12

Re: why is this "not assignable? "

Posted 15 August 2012 - 10:30 AM

View Posthealix, on 15 August 2012 - 10:15 AM, said:

View Postsepp2k, on 15 August 2012 - 09:47 AM, said:

Writing cout << a += b is the same as writing the following:

cout << a;
cout += b;



Do you understand why cout += b can not work?


I understand why

cout += b;


can't work. But what I'm confused about is adding an ostream to the '+=' overload function. How does that work?

That is how the C++ compiler is parsing the line statement: cout << a += b
This is due to the operator precedence in C++: http://en.cppreferen...ator_precedence

View Posthealix, on 15 August 2012 - 10:15 AM, said:

and what about this example from wiki?
Vector2D& Vector2D::operator+=(const Vector2D& right)
{
    this->x += right.x;
    this->y += right.y;
    return *this;
}


What is your question about that example? It's showing how an overload of the += operator is being implemented.
Was This Post Helpful? 2
  • +
  • -

#20 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 10:46 AM

ok I think I see what's going on. Not sure we were on the same page.

In main I can't write
cout << a+=b;



but I can write
 a+=b; 

which works.

so then how would I make it so that it can be written as
 cout << a+=b; 


overload stream, use paranthesis?
Was This Post Helpful? 0
  • +
  • -

#21 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1890
  • View blog
  • Posts: 5,680
  • Joined: 05-May 12

Re: why is this "not assignable? "

Posted 15 August 2012 - 11:13 AM

Although parenthesis will work, just write it out as two lines.
a += b;
cout << a;


Was This Post Helpful? 2
  • +
  • -

#22 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: why is this "not assignable? "

Posted 15 August 2012 - 11:35 AM

ok, thanks everyone. really appreciated
Was This Post Helpful? 0
  • +
  • -

#23 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,185
  • Joined: 05-May 05

Re: why is this "not assignable? "

Posted 15 August 2012 - 12:30 PM

Good catch, sepp2k. I totally missed that.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2