C++中关于流插入运算符和流提取运算符的问题
“<<”和“>>”运算符已经在iostream中被重载,能够用于ostream和istream类对象的标准类型数据的输入输出。但是ostream类和istream类不是i...
“<<”和“>>”运算符已经在iostream中被重载,能够用于ostream和istream类对象的标准类型数据的输入输出。但是ostream类和istream类不是iostream的父类吗?为什么运算符在子类iostream中重载,但是父类的对象却可以直接用<<和>>呢?
请详细解答,谢谢 展开
请详细解答,谢谢 展开
展开全部
看看iostream的源代码就知道了
/*
* Copyright (c) 1998
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
#ifndef __SGI_STL_IOSTREAM
#define __SGI_STL_IOSTREAM
#if defined(__sgi) && !defined(__GNUC__) && !defined(_STANDARD_C_PLUS_PLUS)
#error This header file requires the -LANG:std option
#endif
#include <iosfwd>
#include <istream>
#include <ostream>
__STL_BEGIN_NAMESPACE
// Note: cin and wcin are both associated with stdio. The C standard
// (Amendment 1, section 4.6.2.1) says that it is an error to mix
// wide- and narrow-oriented I/O on the same stream. This implies
// that it is an error to use both cin and wcin in the same C++
// program; the same applies to cout and wcout, and cerr/clog and
// wcerr/wclog.
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
__STL_END_NAMESPACE
#endif /* __SGI_STL_IOSTREAM */
// Local Variables:
// mode:C++
// End:
iostream基本上什么都没有,只是几个extern 并没有重载运算符。ostream和istream也不是它的父类。运算符的重载就是在istream和ostream中。你可以自己看看源代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询