lundi 1 février 2016

Order of operations. F1( int F2( int& x ), int x )

Okay so I was writing some code earlier. This was the line specifically:

EnterNode( FindNode( terrain_X, terrain_Y, travel_dir ), travel_dir );

I noticed after testing my program something weird was happening. The value being received by the outer function was not the value I was reading when I inspected the stack.

I made an example program: http://ift.tt/1PRl6NI

#include <iostream>

using namespace std;

int modifyRef(int& A)
{
    A = 0;
    std::cout << "\nint& A = 0";
    return A;
}

void TakeValues(int X, int Y)
{
    std::cout << "\nX = " << X;
    std::cout << "\nY = " << Y;
}

int main()
{
    int Q = 9;
    TakeValues(modifyRef(Q), Q);
    std::cout << std::endl;
    system("pause");
    return 0;
}

This is the output I receive:

int& A = 0
X = 0
Y = 9

Is this a bug? How is the order of operations defined for parameter binding to function calls?

(My apologies if I am not using the correct terminology, I am primarily self taught)

Aucun commentaire:

Enregistrer un commentaire