lundi 23 septembre 2019

what does it mean by saying that union members can only be manipulted one at a time?

I was just reading up on the theory behind unions and stacks. The book (Object Orientated Programming with C++ by E Balagurusamy) said that "members of a union can only be manipulated one at a time". But I was messing around with unions. I did this with no error.

#include <iostream>
#include <iomanip>
#include "ConsoleApplication1.h"

using namespace std;
//user defined data types
#define writeln(x)(cout<<x<<endl)
union result
{
    int marks;
    char grade;
    float percent;
};
int main()
{  
    result result;
    result.marks = 90;
    result.grade = 'a';
    writeln(result.grade);
    writeln(result.marks);
}

So may you please clarify what that statement meant. Thanks:).

Aucun commentaire:

Enregistrer un commentaire