|
发表于 2019-7-5 23:57:30
|
显示全部楼层
首先,“<<”和“>>”本质上是位运算符,
其之所以能作为流输出输入运算符,是因为在istream和ostream中对其已经进行了重载:- template<class _Elem,
- class _Traits> inline
- basic_istream<_Elem, _Traits>& operator>>(
- basic_istream<_Elem, _Traits>& _Istr, _Elem *_Str)
- { // extract NTBS
- typedef basic_istream<_Elem, _Traits> _Myis;
- typedef ctype<_Elem> _Ctype;
- ios_base::iostate _State = ios_base::goodbit;
- _Elem *_Str0 = _Str;
- const typename _Myis::sentry _Ok(_Istr);
- if (_Ok)
- { // state okay, extract characters
- const _Ctype& _Ctype_fac = _STD use_facet<_Ctype>(_Istr.getloc());
- _TRY_IO_BEGIN
- streamsize _Count = 0 < _Istr.width() ? _Istr.width()
- : (numeric_limits<streamsize>::max)();
- typename _Myis::int_type _Meta = _Istr.rdbuf()->sgetc();
- _Elem _Ch;
- for (; 0 < --_Count; _Meta = _Istr.rdbuf()->snextc())
- if (_Traits::eq_int_type(_Traits::eof(), _Meta))
- { // end of file, quit
- _State |= ios_base::eofbit;
- break;
- }
- else if (_Ctype_fac.is(_Ctype::space,
- _Ch = _Traits::to_char_type(_Meta))
- || _Ch == _Elem())
- break; // whitespace or nul, quit
- else
- *_Str++ = _Traits::to_char_type(_Meta); // add it to string
- _CATCH_IO_(_Istr)
- }
- *_Str = _Elem(); // add terminating null character
- _Istr.width(0);
- _Istr.setstate(_Str == _Str0 ? _State | ios_base::failbit : _State);
- return (_Istr);
- }
复制代码- template<class _Traits> inline
- basic_ostream<char, _Traits>& operator<<(
- basic_ostream<char, _Traits>& _Ostr,
- const char *_Val)
- { // insert NTBS into char stream
- typedef char _Elem;
- typedef basic_ostream<_Elem, _Traits> _Myos;
- ios_base::iostate _State = ios_base::goodbit;
- streamsize _Count = (streamsize)_Traits::length(_Val); // may overflow
- streamsize _Pad = _Ostr.width() <= 0 || _Ostr.width() <= _Count
- ? 0 : _Ostr.width() - _Count;
- const typename _Myos::sentry _Ok(_Ostr);
- if (!_Ok)
- _State |= ios_base::badbit;
- else
- { // state okay, insert
- _TRY_IO_BEGIN
- if ((_Ostr.flags() & ios_base::adjustfield) != ios_base::left)
- for (; 0 < _Pad; --_Pad) // pad on left
- if (_Traits::eq_int_type(_Traits::eof(),
- _Ostr.rdbuf()->sputc(_Ostr.fill())))
- { // insertion failed, quit
- _State |= ios_base::badbit;
- break;
- }
- if (_State == ios_base::goodbit
- && _Ostr.rdbuf()->sputn(_Val, _Count) != _Count)
- _State |= ios_base::badbit;
- if (_State == ios_base::goodbit)
- for (; 0 < _Pad; --_Pad) // pad on right
- if (_Traits::eq_int_type(_Traits::eof(),
- _Ostr.rdbuf()->sputc(_Ostr.fill())))
- { // insertion failed, quit
- _State |= ios_base::badbit;
- break;
- }
- _Ostr.width(0);
- _CATCH_IO_(_Ostr)
- }
- _Ostr.setstate(_State);
- return (_Ostr);
- }
复制代码
而你用不同的方式再次重载了这个符号,对于同一个运算,编译器能够找到不止一个能匹配的重载函数,就会出现定义不明确的错误 |
|