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;
}
7
  • 2
    Welcome to Stack Overflow. Please take the time to read The Tour and refer to the material from the Help Center what and how you can ask here. Commented Mar 13, 2017 at 1: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). At a minimum, you should [edit] your question to include a Minimal, Complete, and Verifiable example that reproduces your problem, along with the observations you made in the debugger. Commented Mar 13, 2017 at 1:43
  • 2
    Perhaps read about Sequence Points. Commented Mar 13, 2017 at 1:48
  • @πάνταῥεῖ It does contain a MCVE. Commented Mar 13, 2017 at 1: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. Commented Mar 13, 2017 at 2:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Thank you for taking the time and adding your comments to the code... I really appreciate it and it was very helpful... thanks Joe

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.