0

Can someone explain why this code gives the output 10? When I try to analyse it my logic gives as result 11.

#include <iostream>
using namespace std; 
class A { 
public:     
    A() { a.a = a.b = 1; }   
    struct { int a,b; } a;  
    int b(void); 
};

int A::b(void) { 
    int x=a.a;
    a.a=a.b;
    a.b=x; 
    return x; 
};

int main(void) {     
    A a;
    a.a.a = 0;
    a.b();
    cout << a.b() << a.a.b << endl;
    return 0;
}
1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49
Monch
  • 1
  • 2
    Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Mar 13 '17 at 01:42
  • 3
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Mar 13 '17 at 01:43
  • 2
    Perhaps read about [Sequence Points](https://en.wikipedia.org/wiki/Sequence_point). – paddy Mar 13 '17 at 01:48
  • @πάνταῥεῖ It does contain a MCVE. – M.M Mar 13 '17 at 01:59
  • @M.M Well, that's a stock comment, including the possibility the OP missed that. I still see no debugging effort s in the question though. – πάντα ῥεῖ Mar 13 '17 at 02:05
  • I would rather see comments that address the specific question, and not stock comments. Also I doubt how useful a debugger is here; it would not give any explanation as to why that order occurred (and it might be difficult to pick up from the debugger when the `a.a.b` was evaluated relative to `a.b()`) – M.M Mar 13 '17 at 02:07
  • I step through my code line-by-line before asking on Stack Overflow!!!, I did analyse it and my logic result was different from the debugger, for that reason I asked to the community... thanks – Monch Mar 14 '17 at 13:29

2 Answers2

2

In the cout line, a.b() could either be called before or after a.a.b is evaluated. Beginners sometimes assume left-to-right evaluation in this sort of code but actually that is not a rule of C++.

Those two different possibilities would explain your 10 and 11. To avoid ambiguity you could write:

cout << a.b();
cout << a.a.b << endl;   

(assuming that order was your intent).

Note: C++17 may change this and define left-right evaluation order for this code.

Community
  • 1
  • 1
M.M
  • 130,300
  • 18
  • 171
  • 314
0

Other than using a debugger, you can also use cout statements to help keep track of when things are called.

To kind of help myself out tracing your program I fixed a bit of the indentation and added comments as to when things are happening:

#include <iostream>
using namespace std; 
class A {
public:
    A() {
        a.a = a.b = 1;
    }
    struct {
        int a,b;
    } a;
    int b(void); 
};

int A::b(void) {
    cout << "Within A::b()" << endl;
    // swap a.a, a.b
    int x=a.a;
    a.a=a.b;
    a.b=x;

    cout << "a.a.a = " << a.a << " a.a.b: " << a.b << endl;

    return x;
};

int main(void) {
    // sets a.a.a = 1, a.a.b = 1
    A a;
    // sets a.a.a = 0, a.a.b = 1
    a.a.a = 0;
    // a.a.a = 1, a.a.b = 0
    a.b();
    // check output here
    cout << a.b() << a.a.b << endl;
    return 0; 
}

The above program results with the following output on http://cpp.sh/ :

Within A::b()
a.a.a = 1 a.a.b: 0
Within A::b()
a.a.a = 0 a.a.b: 1
10

In all, it depends on whether or not a.b() or a.a.b is resolved first when you call cout. In this case, operator precedence is undefined due to how cout works. This stackoverflow post has some good info on this.

Joe Greene
  • 51
  • 5
  • Thank you for taking the time and adding your comments to the code... I really appreciate it and it was very helpful... thanks Joe – Monch Mar 14 '17 at 13:30