基本上,我有重載< <運營商爲我tokenType結構,它遵循的(不能改變,我必須使用這種方式)使用C++工會,當你想要哪個成員是未知
struct tokenType
{
int category ; // one of token categories defined above
union
{
int operand ;
char symbol ; // '+' , '-' , '*' , '/' , '^' , '='
} ;
int precedence() const ;
}
我對重載方法頭是:
ostream & operator<< (ostream & os , const tokenType & tk)
所以,我需要在結構TK打印出來的價值,無論是int或字符。當我不知道變量是操作數還是符號時,如何訪問聯合中包含的內容?謝謝。