一个获取输出流对象的格式问题

B828 2003-10-25 04:25:14
假设我定义一个类:

// XXX.h

#include <iostream>
#include <string>

class XXX
{
public:
...
friend ostream & operator<<(ostream &,const XXX &); // 定义输
//出流重载
// 用来输出abc

private:
string abc;
};

我在主函数中这样调用

#include <iostream>
#include "XXX.h"
using namespace std;

void main()
{
XXX a;

cout<<hex<<a<<endl; // 希望那个字符串变量用hex方式来显示
cout<<dec<<a<<endl; // 希望那个字符串变量用dec方式来显示

....
}

请问我在 friend ostream & operator<<(ostream &,const XXX &);函数中怎么获得otream &的格式信息,根据设置hex和dec的情况来控制输出。

谢谢
...全文
110 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
B828 2003-10-26
  • 打赏
  • 举报
回复
谢谢各位

方法已经找到

用 os.flags() & ios::hex 可以测试是否设置成十六进制
用 os.flags() & ios::dex 可以测试是否设置成十进制

谢谢大家
ch_etang 2003-10-25
  • 打赏
  • 举报
回复
os.flags();
onion_535 2003-10-25
  • 打赏
  • 举报
回复
同意~!
ttlb 2003-10-25
  • 打赏
  • 举报
回复
呵呵,搂主已经写出来啦,就这样就可以。 cout<<hex<<a<<endl;
hex 会被自动传到 operator<<(ostream &,const XXX &);

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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