请问C++大咖们如何自定义一个有类似cout功能的对象啊?

朱并作 2018-07-10 09:56:00
请问C++大咖们如何自定义一个类似cout的对象啊? 现在我在学习c++,想自定义一个功能类似cout的对象,比如对象shuchu,
实现shuchu<<"hello word"<<endl; 这样的功能。
我是这样写代码的,但是好像不能成功。恳请大咖们指教。
#include <iostream>
#include<ostream>
using namespace std;
int main()
{
ostream_withassign shuchu;
shuchu<< "Hello world!" << endl;
return 0;
}
...全文
250 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-07-10
  • 打赏
  • 举报
回复
提醒:cout是开源的。
百合杰 2018-07-10
  • 打赏
  • 举报
回复
#define shuchu cout
jena_wy 2018-07-10
  • 打赏
  • 举报
回复
typedef basic_ostream<char, char_traits<char> > ostream;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream cout;



template<class _Elem,
class _Traits>
class basic_ostream
: virtual public basic_ios<_Elem, _Traits>
{ // control insertions into a stream buffer
public:
typedef basic_ostream<_Elem, _Traits> _Myt;
typedef basic_ios<_Elem, _Traits> _Myios;
typedef basic_streambuf<_Elem, _Traits> _Mysb;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef num_put<_Elem, _Iter> _Nput;

explicit __CLR_OR_THIS_CALL basic_ostream(basic_streambuf<_Elem, _Traits> *_Strbuf,
bool _Isstd = false)
{ // construct from a stream buffer pointer
_Myios::init(_Strbuf, _Isstd);
}

__CLR_OR_THIS_CALL basic_ostream(_Uninitialized, bool _Addit = true)
{ // construct uninitialized
if (_Addit)
ios_base::_Addstd(this); // suppress for basic_iostream
}

virtual __CLR_OR_THIS_CALL ~basic_ostream()
{ // destroy the object
}

typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;

class _Sentry_base
{ // stores thread lock and reference to output stream
public:
__CLR_OR_THIS_CALL _Sentry_base(_Myt& _Ostr)
: _Myostr(_Ostr)
{ // lock the stream buffer, if there
if (_Myostr.rdbuf() != 0)
_Myostr.rdbuf()->_Lock();
}

__CLR_OR_THIS_CALL ~_Sentry_base()
{ // destroy after unlocking
if (_Myostr.rdbuf() != 0)
_Myostr.rdbuf()->_Unlock();
}

_Myt& _Myostr; // the output stream, for _Unlock call at destruction
};

class sentry
: public _Sentry_base
{ // stores thread lock and state of stream
public:
explicit __CLR_OR_THIS_CALL sentry(_Myt& _Ostr)
: _Sentry_base(_Ostr)
{ // construct locking and testing stream
if (_Ostr.good() && _Ostr.tie() != 0)
_Ostr.tie()->flush();
_Ok = _Ostr.good(); // store test only after flushing tie
}

__CLR_OR_THIS_CALL ~sentry()
{ // destroy the object

#if _HAS_EXCEPTIONS
if (!_XSTD uncaught_exception())
this->_Myostr._Osfx();
}

#else /* _HAS_EXCEPTIONS */
this->_Myostr._Osfx();
}
#endif /* _HAS_EXCEPTIONS */

__CLR_OR_THIS_CALL operator bool() const
{ // test if stream state okay
return (_Ok);
}

private:
__CLR_OR_THIS_CALL sentry(const sentry&); // not defined
sentry& __CLR_OR_THIS_CALL operator=(const sentry&); // not defined

bool _Ok; // true if stream state okay at construction
};

bool __CLR_OR_THIS_CALL opfx()
{ // test stream state and flush tie stream as needed (retained)
if (ios_base::good() && _Myios::tie() != 0)
_Myios::tie()->flush();
return (ios_base::good());
}

void __CLR_OR_THIS_CALL osfx()
{ // perform any wrapup (retained)
_Osfx();
}

void __CLR_OR_THIS_CALL _Osfx()
{ // perform any wrapup
_TRY_BEGIN
if (ios_base::flags() & ios_base::unitbuf)
flush(); // flush stream as needed
_CATCH_ALL
_CATCH_END
}

#ifdef _M_CEE_PURE
_Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__clrcall *_Pfn)(_Myt&))
{ // call basic_ostream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}

_Myt& __CLR_OR_THIS_CALL operator<<(_Myios& (__clrcall *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(ios_base& (__clrcall *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}

#endif

_Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__cdecl *_Pfn)(_Myt&))
{ // call basic_ostream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}

_Myt& __CLR_OR_THIS_CALL operator<<(_Myios& (__cdecl *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(ios_base& (__cdecl *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(_Bool _Val)
{ // insert a boolean
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(short _Val)
{ // insert a short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
ios_base::fmtflags _Bfl =
ios_base::flags() & ios_base::basefield;
long _Tmp = (_Bfl == ios_base::oct
|| _Bfl == ios_base::hex)
? (long)(unsigned short)_Val : (long)_Val;

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Tmp).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

/* Note that if your stream is wchar_t, and you are not using native wchar_t
Then this operation will be unavailable as there is an explicit
specialisation further down this file that is designed to treat an
unsigned short as a character.

If you wish to read or write unsigned shorts to wchar_t streams, you should
consider making wchar_t a native type by turning on /Zc:wchar_t
*/
_Myt& __CLR_OR_THIS_CALL operator<<(unsigned short _Val)
{ // insert an unsigned short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (unsigned long)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(int __w64 _Val)
{ // insert an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
ios_base::fmtflags _Bfl =
ios_base::flags() & ios_base::basefield;
long _Tmp = (_Bfl == ios_base::oct
|| _Bfl == ios_base::hex)
? (long)(unsigned int)_Val : (long)_Val;

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Tmp).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(unsigned int __w64 _Val)
{ // insert an unsigned int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (unsigned long)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(long _Val)
{ // insert a long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(unsigned long __w64 _Val)
{ // insert an unsigned long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (unsigned long)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

#ifdef _LONGLONG
_Myt& __CLR_OR_THIS_CALL operator<<(_LONGLONG _Val)
{ // insert a long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(_ULONGLONG _Val)
{ // insert an unsigned long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}
#endif /* _LONGLONG */

_Myt& __CLR_OR_THIS_CALL operator<<(float _Val)
{ // insert a float
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);

_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (double)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}

_Myios::setstate(_State);
return (*this);
}

_Myt& __CLR_OR_THIS_CALL operator<<(double _Val)
{ // insert a double
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(),
赵4老师 2018-07-10
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
提醒:cout是开源的。

C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\src\crt\iostream
C:\Program Files\Microsoft Visual Studio\VC98\Include\crt\iostream
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\iostream
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\iostream
C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\iostream
C:\Program Files\Microsoft Visual Studio 8\VC\include\iostream
C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\iostream
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\iostream
随便打开哪个一眼都行。
朱并作 2018-07-10
  • 打赏
  • 举报
回复
#define shuchu cout 这个是宏替换啊, 这个太没技术含量了吧。我是想实现声明一个全新的类对象。这个类对象的功能和cout是相同的。我想搞清楚C++中的cout对象到底是怎么定义的,能不能自己声明一个实现输出的对象。不好意思,本人有点强迫症,有些东西不弄清楚,心里就不爽。
weixin_42359982 2018-07-10
  • 打赏
  • 举报
回复
引用 1 楼 kobehahaha 的回复:
#define shuchu cout

老太太都不敢扶,就扶你
忘世麒麟 2018-07-10
  • 打赏
  • 举报
回复
引用 1 楼 kobehahaha 的回复:
#define shuchu cout

谁都不扶,就扶你!

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧