C++ “你是怎么做的?”<<&引用;及&燃气轮机&燃气轮机&引用;操作员是否执行I/O?
可能重复:C++ “你是怎么做的?”<<&引用;及&燃气轮机&燃气轮机&引用;操作员是否执行I/O?,c++,C++,可能重复: 我期待着回到C++,还有一些基本的符号,在其他语言中似乎并不那么突出。 如果你看这行代码 cout << "firstvalue is " << firstvalue << endl; coutC++允许运算符重载。这意味着用户定义的类型可以在内置运算符上定义自己的行为。在这种情况下,运算符被称为:左移或右移运算符。这些运算符传统上用于位移位,但标准库将其重新调整用途,以象征流式操作 您可以在中找到可用运算符的列表 在本例中,您正在将字符串文本
我期待着回到C++,还有一些基本的符号,在其他语言中似乎并不那么突出。 如果你看这行代码
cout << "firstvalue is " << firstvalue << endl;
coutC++允许运算符重载。这意味着用户定义的类型可以在内置运算符上定义自己的行为。在这种情况下,运算符被称为:左移
或右移
运算符。这些运算符传统上用于位移位,但标准库将其重新调整用途,以象征流式操作
您可以在中找到可用运算符的列表
在本例中,您正在将字符串文本和某个类型的值流式传输到类型为的对象中
一步一步
应用优先规则后,您的代码如下所示:
((cout << "foobar") << x) << endl;
((cout这是对以下内容的语法补充:
// Let the function 'print' be a renaming of 'operator<<'
// with T being the type of the object you want to print.
std::ostream& print(std::ostream&, const T&);
// 1) Print "first value is" and then return the stream you
// to which to just printed (ie. cout). 2) Use the returned
// stream to chain function calls and print 'firstValue'.
print(print(std::cout, "first value is"), firstValue);
//让函数“print”是“operator”的重命名它们被称为流插入(或提取,在istream>
的情况下),实际上是左移位和右移位运算符的语义重载
那么这个,
int x = 1 << 1;
<代码> int x=1>p> <代码> < >代码>强制阅读:在这个上下文中,<代码>代码> cOUT >代码>不是一个函数。它是一个对象,对于<代码>如果这个站点是C++,这个站点是一个非常宝贵的参考,你要找的答案是:超文本字符串不是一个成员,而是一个O。f我链接的自由函数。
int x = 1 << 1;
std::cout << x;
operator <<(std::cout, x);
std::ostream& operator <<(std::ostream&, T value);
operator<< (
operator<< (
operator<<(std::cout, "firstvalue"),
firstvalue
),
std::endl
);
3 << 2
00000011
00001100
std::cout << "whatever"
std::cout << f() << g() << h();
5 + 3 + 2
((5 + 3) + 2)
std::cout << "Hello" << std::endl
((std::cout << "Hello") << std::endl)