Why is ostream::operator<< a global function for char parameters?
문제
Acording to http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ the operator<< method defined on e.g. streambuf is a member of ostream, but for char / char * it is a global function. What's the design decision behind this difference?
해결책
operator<<
for streambuf*
(or int
which sounds a simpler case) and char
could have been both implemented as member operators, or as non-member (free) operators.
My guess is that it's due to retro-compatibility issues emerged while C++ was being defined: maybe older code was relaying on a member operator<<(int)
, and thus they decided not to move it as a free operator.
The C++ standard library (and also STL) have a number of dishomogeneity like this one.
제휴하지 않습니다 StackOverflow