0

从头文件中可以看出<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() 更改这些标志?他们不是恒定的吗?

4

2 回答 2

1

如果您查看 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

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

于 2012-12-31T12:15:46.503 回答
0

答案是这些成员没有改变;setf设置一些非常量成员(可能是私有的/受保护的)。您可能可以检查相关的标头以准确了解您的实现是做什么的。

于 2012-12-31T12:10:26.877 回答