Why are bitwise shifts (<< and>>) used for cout and cin?

后端 未结 13 1579
眼角桃花
眼角桃花 2020-12-02 16:16

Question is in the title really; I\'m sure there is something logical, but for now I\'m stumped!

相关标签:
13条回答
  • 2020-12-02 17:03

    According to §8.3.1 of The Design and Evolution of C++:

    The idea of providing an output operator rather than a named output function was suggested by Doug McIlroy by analogy with the I/O redirection operators in the UNIX shell (>, >>, |, etc.)

    [...]

    Several operators were considered for input and output operations: the assignment operator was a candidate for both input and output, but it binds the wrong way. That is cout=a=b would be interpreted as cout=(a=b), and most people seemed to prefer the input operator to be different from the output operator. The operators < and > were tried, but the meanings "less than" and "greater than" were so firmly implanted in people's minds that the new I/O statements were for all practical purposes unreadable (this does not appear to be the case for << and >>). Apart from that, '<' is just above ',' on most keyboards, and people were writing expressions like this:

    cout < x , y, z;
    

    It is not easy to give good error messages for this.

    0 讨论(0)
  • 2020-12-02 17:03
    cout << "Output sentence"; // prints Output sentence on screen
    cout << 120;               // prints number 120 on screen
    cout << x;                 // prints the content of x on screen 
    

    The << operator inserts the data that follows it into the stream preceding it. In the examples above it inserted the constant string Output sentence, the numerical constant 120 and variable x into the standard output stream cout.

    The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example:

    int age;
    cin >> age;
    
    0 讨论(0)
  • 2020-12-02 17:04

    >> and << are just operators and you can implement your own >> and << for your classes.

    I suppose "somebody" selected them because: a) they are similar to shell file operations and b) to reuse existing operators because there are no need to create new ones

    0 讨论(0)
  • 2020-12-02 17:09

    I assume that you are aware that C++ allows for operator overloading. In general, you overload operators only if the semantics are completely transferable (e.g. overloading the addition for a vector class to add two vectors together). I think your question refers to why one would use bitshift operators, overload them for the iostream, and give them a completely different meaning than their original purpose. The reason it can be done is because bitshift operations are so far removed from what iostreams do that no one could be confused into thinking that << or >> is doing a bitshift on an iostream. And the reason why they are convenient to use also is that their ordering is to evaluate the operand on the left first, then the one on the right, and do the operation. This fits to what you would want to happen when you are using the operators to append or extract stuff from an iostream.

    But, to the original question, why? I don't really know, it just seems to me like the << and >> are pretty easily understood as taking information from one entity and putting it in the other. Why does the reason need to be more complicated than that? It looks sensible to use those because their meaning is obvious.. what better could you ask of an operator?

    0 讨论(0)
  • 2020-12-02 17:12

    This answer is unsatisfying but correct: they aren't bitwise operators.

    The meaning of the operator is determined by the data-type that appears on its left. In the case of cin and cout (and other stream types) << and >> operators move values to and from streams. In the case that the left operand is an integer, the operation is the bitwise operation that you already know from C.

    The meaning of the operator is not fixed, although its precedence is.

    0 讨论(0)
  • 2020-12-02 17:15

    From "The C++ Programming language". Stroustrup's(language authors) words:

    Overloading the operator << to mean ‘‘put to’’ gives a better notation and lets the programmer output a sequence of objects in a single statement.

    But why <<? It is not possible to invent a new lexical token . The assignment operator was a candidate for both input and output, but most people seemed to prefer to use different operators for input and output. Furthermore, = binds the wrong way; that is, cout=a=b means cout=(a=b) rather than (cout=a)=b . I tried the operators < and >, but the mean ‘‘less than’’ and ‘‘greater than’’ were so firmly implanted in people’s minds that the new I/O statements were for all practical purposes unreadable.

    0 讨论(0)
提交回复
热议问题