dimanche 23 octobre 2016

c++ Modulo assignment % don't work in union

When trying to do a modulo assignment when executing a function inside a union it just doesn't the assignment, but out of the union does, why?

union getd
{
    int getval(int abc)
    {
        return 1;
    }
};

int main() {
    int x = 0;
    getd test;
    for(int i=0; i <= 4; i++)
    {
        x++;
        test.getval(x%2);
        cout << x << endl;
    }
    x=0;
    for(int i=0; i <= 4; i++)
    {
        x++;
        cout << x%2 << endl;
    }
    return 0;
}

Output of the first for should be the same as the second, and is not, the first:

1
2
3
4
5

Second for (right output):

1
0
1
0
1

Aucun commentaire:

Enregistrer un commentaire