gpt4 book ai didi

c++ - `ios_base` 的标志都是静态常量整型变量。为什么可以用 `ios_base::setf()` 改变它们?

转载 作者:行者123 更新时间:2023-11-28 07:49:43 28 4
gpt4 key购买 nike

头文件中可以看出<xiosbase> , 类 ios_base源自 template <class Dummy> class _Iosb ,其中以下 static const variables定义:

 static const _Fmtflags skipws = (_Fmtflags)_IOSskipws;
static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf;
static const _Fmtflags uppercase = (_Fmtflags)_IOSuppercase;
static const _Fmtflags showbase = (_Fmtflags)_IOSshowbase;
static const _Fmtflags showpoint = (_Fmtflags)_IOSshowpoint;
static const _Fmtflags showpos = (_Fmtflags)_IOSshowpos;
static const _Fmtflags left = (_Fmtflags)_IOSleft;
static const _Fmtflags right = (_Fmtflags)_IOSright;
static const _Fmtflags internal = (_Fmtflags)_IOSinternal;
static const _Fmtflags dec = (_Fmtflags)_IOSdec;
static const _Fmtflags oct = (_Fmtflags)_IOSoct;
static const _Fmtflags hex = (_Fmtflags)_IOShex;
static const _Fmtflags scientific = (_Fmtflags)_IOSscientific;
static const _Fmtflags fixed = (_Fmtflags)_IOSfixed;

为什么可以使用成员函数 ios_base::setf() 更改这些标志?它们不是常数吗?

最佳答案

如果你研究 GCC 的实现,ios_base:setf 看起来像这样

仅添加版本

   inline fmtflags              
setf(fmtflags __fmtfl)
{
fmtflags __old = _M_flags;
_M_flags |= __fmtfl;
return __old;
}

添加/删除版本

inline fmtflags                           
setf(fmtflags __fmtfl, fmtflags __mask)
{
fmtflags __old = _M_flags;
_M_flags &= ~__mask;
_M_flags |= (__fmtfl & __mask);
return __old;
}

即它改变了成员变量_M_flags

您正在查看的静态常量变量只是该变量的方便预定义值。

关于c++ - `ios_base` 的标志都是静态常量整型变量。为什么可以用 `ios_base::setf()` 改变它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14101391/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com