cout 的输出根据类型转换方式而变化
我正在开发一个程序,遇到一个奇怪的与 cout 相关的问题。由于程序有点大,而且代码最好讲,我将粘贴相关片段。
首先,我有一个迭代器,在 for 中定义为
for(vector
在此 for 中,如果我使用表达式,则
facet* facet_to_work_on = *it;
cout << facet_to_work_on->facet_id << "\t";
可以很好地打印出整数。
但是,如果我使用符号
cout << (facet*)(*it)->facet_id << "\t";
此代码会打印出十六进制值。十六进制值等于整数值。知道为什么会发生这种情况吗?
提前致谢。
I'm working on a program and have a strange, cout related problem. Since the program is a bit big and the code talks best, I'll paste the relevant snippets.
First, I have an iterator, *it
defined in a for as
for(vector<facet*>::iterator it=facets_to_dump->begin(); it<facets_to_dump->end(); it++)
In this for, if I use the expression
facet* facet_to_work_on = *it;
cout << facet_to_work_on->facet_id << "\t";
Nicely prints out integers.
But, if I use the notation
cout << (facet*)(*it)->facet_id << "\t";
This code prints out hex values. Hex values are equal to the integer values. Any idea why this is happening?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打印出十六进制值的原因
是 ->绑定比 (facet*) 转换更难,即它评估
并将结果转换为facet*。指针以十六进制输出。
The reason
prints out a hex value is that -> binds harder than the (facet*) cast, that is it evaluates
and casts the result to a facet*. Pointers are output in hex.
尝试包含
和:表示您想要十进制形式的数字。
Try including
<iomanip>
and:To say you want numbers in decimal form.