简体   繁体   English

这个权利转移如何工作:stringstream >> unsigned int >> unsigned int?

[英]How does this right shifts work: stringstream >> unsigned int >> unsigned int?

Im working with the book SFML Game Development by Examples and I dont really get what this sentence does. 我正在使用例子SFML游戏开发的书,我真的不知道这句话的作用。 I've never seen something like this 我从来没有见过这样的东西

void Anim_Directional::ReadIn(std::stringstream& l_stream){
l_stream >> m_frameStart >> m_frameEnd >> m_frameRow
  >> m_frameTime >> m_frameActionStart >> m_frameActionEnd;
}

In C++ they got the "bright" idea of overloading the rightshift and leftshift operators with streams to represent serialization/deserialization. 在C ++中,他们获得了“明亮”的想法,即使用流来重载右移和leftshift运算符以表示序列化/反序列化。

stream >> var

means "read var from stream". 表示“从流中读取var”。

Symmetrically 对称地

stream << var

mean "put var into stream" 意思是“把变成流”

The operation of "streaming" in or out also returns the stream, so you can chain operations like: “流”输入或输出的操作也会返回流,因此您可以链接以下操作:

stream >> var1 >> var2;

Note that the "streaming" was chosen just because of the look and because the priority was considered reasonable, but it's still just an overloaded operator and implies for example no strict sequence of evaluation. 请注意,选择“流式传输”只是因为外观和优先级被认为是合理的,但它仍然只是一个重载的运算符,并暗示例如没有严格的评估序列。 For example in: 例如:

stream << f() << g();

may be function g is called (somewhat surprisingly) before function f . 可能是函数g在函数f之前被调用(有些令人惊讶)。

NOTE: the sequencing problem was handled by hammering this special case in last C++ standard (C++17). 注意:在最后的C ++标准(C ++ 17)中通过锤击这种特殊情况来处理排序问题。 While it doesn't hold in general it's guaranteed for shift operators (presumably for this specific reason). 虽然它一般不成立,但它保证了移位运营商(可能是出于这个特定原因)。 So in f()+g() may be f is called later than g , but in f()<<g() this cannot happen. 因此,在f()+g()f被称为迟于g ,但f()<<g()这是不可能发生的。

C++ allows you to overload >> and << operators. C ++允许您重载>><<运算符。 std::stringstream is a derivative of std::istream and it inherits the >> operator overloads of std::istream . std::stringstream是衍生物std::istream和它继承了>>的操作符重载std::istream

The std::istream has a bunch of overloads for many common types. std::istream对许多常见类型都有一堆重载。 You can find a list of them here . 你可以在这里找到它们的列表。

A typical std::istream >> operator overload looks as follows: 典型的std::istream >>运算符重载如下所示:

std::istream& operator>>(std::istream& stream, YourType& var) {
    /* 
    ** code here to parse and read a 'YourType' into 'var'
    */
    /* var is set */
    return stream; /* return the same stream to allow chaining */
}

When you do some_stream >> YourType_object , the matching >> operator overload is invoked. 当您执行some_stream >> YourType_object ,将调用匹配的>>运算符重载。 In the aforementioned case, our operator overload is invoked with stream parameter taking some_stream and var taking YourType_object . 在前面提到的情况下,我们的运算符重载是使用stream参数调用some_streamvar获取YourType_object

The >> overloads (and << overloads too) intelligently return the stream which they operated; >> overloads(和<< overloads)也会智能地返回它们运行的​​流; thereby, allowing a series of >> operators to be chained. 从而允许一系列>>运营商被链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM