作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
基本上,我必须为我的 tokenType 结构重载 << 运算符,如下所示(无法更改,我必须以这种方式使用它)
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)
因此,我需要打印出 struct tk 中的值,int 或 char。当我不知道变量是操作数还是符号时,如何访问 union 中包含的内容?谢谢。
最佳答案
您需要做的是查看 category
成员(它不是 union 的一部分)来决定使用哪个 union 元素。像下面这样的东西可能会有用(显然我在猜测类别定义):
switch (tk.category) {
case catOperand:
os << tk.operand;
break;
case catSymbol:
os << tk.symbol;
break;
}
关于c++ - 使用 C++ union ,当你想要的成员未知时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117558/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!