为什么显式调用operator<< on std :: cout导致意外输出? [英] Why does explicitly calling operator<< on std::cout cause unexpected output?

查看:322
本文介绍了为什么显式调用operator<< on std :: cout导致意外输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇如果我在 std :: cout 上调用 operator 因为我知道 a.operator() a()完全相同。所以我做它,它打印一些奇怪的:

I was simply curious about what would happen if I called operator<< on std::cout explicitly because I learnt that a.operator() is exactly the same as a(). So I do it and it prints something weird:

#include <iostream>

using std::cout;

int main()
{
    cout.operator<<("Hello World");
}




输出:0x80486a0

奇怪的是,它会输出一个地址(地址可能会有所不同,地址)。我认为这是字符串的地址,所以我尝试解除引用它,以得到它输出字符串:

Oddly, it outputs an address (the address may be different for you but it should still be an address). I'm thinking this is the address of the string so I try dereferencing it to get it to output the string:

*( cout.operator<<("Hello World") );

但我遇到了很长的错误

无法匹配操作符* in'* std :: cout.std :: basic_ostream< ...

我认为这很奇怪。 std :: cout 定义不会导致我相信这会导致任何不同的行为;也给出了显式调用运算符函数没有差别(或至少应该)的事实。

I think this is pretty weird. Nothing of the std::cout definition would lead me to believe this would cause any different behavior; also given the fact that explicitly calling the operator function makes no difference (or should at least).

那么为什么我得到这个输出?为什么我明确地调用操作符时接收地址而不是字符串本身?这是甚至是内存中的地址或只是垃圾输出?

So why am I getting this output? Why am I receiving an address instead of the string itself when calling the operator explicitly? Is this even the address in memory or just garbage output? Any responses are appreciated.

推荐答案

内置字符串的输出运算符,即采用 char const * 作为参数,不是 std :: ostream 的成员。运行 char const * 的运算符是非成员函数,将被称为

The output operator for built-in strings, i.e., the on taking a char const* as argument, isn't a member of std::ostream. The operator taking a char const* is a non-member function would be called as

operator<< (std::cout, "Hello World");

但是,有一个成员需要一个 void const * ,它使用十六进制符号格式化指针的值。这个成员是明确传递任何指针到成员运算符<<

There is, however, a member taking a void const* which formats the value of the pointer using hex notation. This member is the best match when passing any pointer explicitly to a member operator<< () of std::ostream.

取消引用 std :: ostream code>运算符<<()不工作:运算符返回一个 std :: ostream& 一元运算符*()重载。如果你想引用参数,你可以像这样调用:

Dereferencing the results of a the operator<<() doesn't work: The operators return a std::ostream& which doesn't have a unary operator*() overloaded. If you meant to dereference the argument, you'd call it like so:

std:cout.operator<< (*"Hello World");

但是,这只是参照 char const * 字符串字面值衰减到,产生个别字符 H 。字符输出函数不是成员函数,而整数的输出运算符是,即,它将打印字符值 H 。对于使用ASCII的系统,它将 72

However, this would just derference the char const* the string literal decays to, yielding an individual character H. The character output function isn't a member function, either, while the output operators for integers are, i.e., it would print character value of H. For a system using ASCII it would be 72.

这篇关于为什么显式调用operator&lt;&lt; on std :: cout导致意外输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆