When I executed the following code the output appears to be very strange to me. Can any one explain this unexpected result to me.

The code:

#include <iostream>
using namespace std;

class Y
{
private:
       int x;
public:
     Y()
     {
          x = 7;
     }
     
      int getX()
     {
           return x;
     }

     int setX(  int s )
     {
           x = s;
           return x;
     }
};


void  main()
{
       Y myY;
       cout<< ( myY.setX(12) ) << ' ' << ( myY.getX() ) << endl;  //1
       int m;
       cout<< (m=10) << ' ' << (m=12) << ' ' << (m=14) << endl; //2
}

The first 'cout' prints:
12 7
But it should ouput:
12 12

The second 'cout' prints:
10 10 10
But it should output:
10 12 14

I'm using c++ dotNet 2005 student edition win 32 console application.

Please fully explain this behaviour to me.

Thanks in advance

WaelTheNoble

Recommended Answers

All 7 Replies

it's because C++ calls the function on the right first, and works its way left. Just add a couple of cout's in your two methods, and see the order in which they are called... getX is called, and then setX is called:

#include <iostream>

using namespace std;

class Y
{
	private:
		int x;
	
	public:
		Y() { this->x = 7; }

		int getX() 
		{
			cout << "in getX()" << endl;
			return this->x;
		}

		int setX( int s )
		{
			this->x = s;
			cout << "in setX" << endl;
			return this->x;
		}
};

int main(int argc, char **argv)
{
	Y myY;
	cout << myY.setX(12) << ' ' << myY.getX() << endl;

	return 0;
}

The fix is to simply put them on lines by themselves, such as:

cout << myY.setX(12) << ' ';
cout << myY.getX() << endl;

As for the second section... I'm not quite sure about that one.

Thanks a lot for your quick reply but I think that the 2 cases I mentioned are a bugs in cout (They have no reasonable meaning to me), and also I think that C++ don't process functions from right to left as you mentioned if you write the following code in the past file:

int tr;
tr = myY.setX(10) + myY.setX(12);

it will be processed from left to right

The order of evaluation of operands of the operator << is unspecified by the C++ Standard. Therefore both cases in the original snippet are ill-formed. It's another case of the famous bad code example a[i] = ++i; ;)
The order of operands evaluation (from left to right) is defined for comma operator, operator && and operator || only...

commented: Fantastic Insight +10

>The order of operands evaluation (from left to right) is defined
>for comma operator, operator && and operator || only...
But only the built-in version. When you overload them yourself, you lose the order of evaluation guarantee, which is why it's not recommended that you overload those particular operators.

>I think that the 2 cases I mentioned are a bugs in cout
Q: How many legs does a dog have if you call the tail a leg?
A: Four. Calling a tail a leg doesn't make it a leg.

Thanks a lot for your reply but can I know from where can I have more information about bad coding and C++ standards. And if it is possible for me to have a citation from you about the non-standard left to right processing of the operators +, <<, --, etc, I would be grateful.

Thanks a lot to all of you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.