- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
为什么三个运算符<<输出方式不同?
#include <iostream>
#include <string>
using namespace std;
int main()
{
operator<<(cout, "Hello").operator<<('w').operator<<(endl); // Hello119
cout << (void *)'w' << endl; // 0x77
cout << 'w'; // w
operator<<(operator<<(cout, "Hello"), 'w').operator<<(endl); // Hellow !!!
}
带有 2 个参数的第一个运算符<<按预期为字符串提供了正确的输出,而其他运算符则没有...
有人可以向我解释一下吗?
更多信息:
cout << "Hello" << 'w' << endl;
将被解释为...
operator<<(operator<<(cout, "Hello"), 'w').operator<<(endl); // Hellow
最佳答案
您正在强制转换为 void
指针和 std::ostream& operator<<(std::ostream&,void*);
的特化,因此您得到的输出是十六进制值而不是十进制值。
注意:0x77 == 119
另请注意:
您正在调用 std::ostream::operator<<()
成员运算符在这里重载:
operator<<(cout, "Hello").operator<<('w').operator<<(endl);
// ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
// | |
// | + Calls the std::ostream::operator<<() member operator
// + Calls the std::ostream& operator(std::ostream, T) global overload
char
没有过载, 因此隐式转换为 int
取数值119
出现在输出中。
来自上述 std::ostream::operator<<()
的链接引用成员函数:
std::cout << 'w' << 'w' << 'w' << std::endl;
将推导为全局 std::ostream& operator<<(std:: ostream&, T)
的链式调用过载:
operator<<(operator<<(operator<<(operator<<(std::cout,'w'),'w'),'w'),std::endl);
关于c++ operator<<(char) 但在 hexa/int 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054971/
我有一个名为 const char receiptLogo[] = {0x01,0x80,0x00,0xB4}; 当我试图获取值时 Rprintf("%x\r\n",receiptLogo[3]);
面对 pysnmp 代理返回值的奇怪问题。 PYSNMP 代理接收字符串格式为 "0a0a0a0a0a0a0a0a0a" . 在 MIB 中,OID 表示为 HEXA STRING。 当尝试使用 rf
我正在尝试将颜色值更改为颜色hexa代码。所以,我的代码是这样的: color = Integer.toHexString(colorpick.getValue().hashCode()).subst
为什么三个运算符 #include using namespace std; int main() { operator<<(cout, "Hello").operator<<('w').ope
我正在学习理解线程转储,并且创建了一个示例应用程序来模拟死锁条件。 以下是完整代码: Thread1.java package com.example.threads.deadlock; public
我已经使用 Flask 在本地服务器中创建了一个 Restfull API。 我在六边形的编码/解码方面遇到了一些麻烦。 我创建了一个函数来调试我的问题: class test_encoding(Re
我正在使用 Java 8 和 I18N。根据我的理解,.properties 文件(以及后续的 I18N 代码)假定文件采用“ISO-8859-1”文件格式。因此,我遇到了无法以该文件格式表示的字符的
正在将 MongoDB 迁移到 postgres。 感谢 postgres JSON ,这使得将嵌套文档作为 JSON 移动变得容易。但问题始于迁移“ID”字段。 Mongo 生成一个大的十六进制数
我需要在 RS232 端口(串行)上使用指纹进行对话。我有一个非常有用的 python 代码: import serial import time ser = serial.Serial('/dev/
我想使用 HEXA 包发送 MIDI 音符。 G2、D♯3、D3 快速使用 coreMIDI 最佳答案 请参阅下面的 Midi 数据包以获取上述注意事项。 G2 var packet1:MID
我正在研究 iOS MDM。在注册期间,我获得了 deviceToken、PushMagic 和 unlockToken。计划使用javapns .它期望 64 十六进制值。 这是我原来的设备 tok
我是一名优秀的程序员,十分优秀!