使用<<带有指向 char 的指针的运算符

发布于 2024-12-03 11:52:05 字数 101 浏览 4 评论 0 原文

<< 有一个运算符重载,它将 char* 解释为 C 样式字符串。但是,如果 char* 指向单个字符,如何将其输出到流中?

There is an operator overload for << which interprets char* as a C-style string. If char* however points to a single character, how do I output it into a stream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野心澎湃 2024-12-10 11:52:05

如果 ch 的类型为 char* 并且实际上指向单个 char,请使用 *ch 以便表达式的类型为 字符。操作员就会知道该怎么做。

If ch is of type char* and actually points to a single char, use *ch so that the type of the expression is char. The operator will know what to do.

挖个坑埋了你 2024-12-10 11:52:05
char c=42;//sadly I could not use my favourite constant 666
char* pc=&c;
cout << *pc;

char c=42;//sadly I could not use my favourite constant 666
char* pc=&c;
cout << *pc;

权谋诡计 2024-12-10 11:52:05
char x = 'a';
char *p = &x;
stream << *p;
char x = 'a';
char *p = &x;
stream << *p;
时光礼记 2024-12-10 11:52:05

对于这种情况,您需要为 char 重载 << (无 *)并取消引用您的 char* 在表达式中使用它之前。

For that case you'd need to overload << for char (no *) and dereference your char* before using it in the expression.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文