小编rfm*_*as3的帖子

使用C++联合,何时需要哪个成员是未知的

基本上,我必须为我的tokenType结构重载<<运算符,后面跟着(不能更改,我必须以这种方式使用它)

struct tokenType 
{
    int category  ;   // one of token categories defined above
    union 
    {
        int  operand ;
        char symbol ; // '+' , '-' , '*' , '/' , '^' , '='
    } ;
    int  precedence() const ;
}
Run Code Online (Sandbox Code Playgroud)

我的重载方法的标题是:

ostream & operator<< ( ostream & os , const tokenType & tk)
Run Code Online (Sandbox Code Playgroud)

所以,我需要打印出struct tk中的值,int或char.当我不知道变量是操作数还是符号时,如何访问union中包含的内容?谢谢.

c++ operator-overloading ostream unions

4
推荐指数
1
解决办法
1441
查看次数

标签 统计

c++ ×1

operator-overloading ×1

ostream ×1

unions ×1