std:string类
时间:2010-08-14 来源:xulinyu
string类包含
npos
allocator
_Ptr
_Len
_Res
五个成员变量。在XP2,VC6.0下sizeof(string)==16。
看string源代码的时候发现成员函数assign中的一个是这样的
_Myt& assign(const _Myt& _X, size_type _P, size_type _M)
{if (_X.size() < _P)
_Xran();
size_type _N = _X.size() - _P;
if (_M < _N)
_N = _M;
if (this == &_X)
erase((size_type)(_P + _N)), erase(0, _P);
else if (0 < _N && _N == _X.size()
&& _Refcnt(_X.c_str()) < _FROZEN - 1
&& allocator == _X.allocator)
{_Tidy(true);
_Ptr = (_E *)_X.c_str();
_Len = _X.size();
_Res = _X.capacity();
++_Refcnt(_Ptr); }
else if (_Grow(_N, true))
{_Tr::copy(_Ptr, &_X.c_str()[_P], _N);
_Eos(_N); }
return (*this); } 这个成员函数还调用了++_Refcnt(_Ptr); 这个函数,看下他是怎样的 unsigned char& _Refcnt(const _E *_U)
{return (((unsigned char *)_U)[-1]); } 可以发现_Ptr是从-1开始的,_Ptr[-1]保存的是_Ptr的引用计数。
{if (_X.size() < _P)
_Xran();
size_type _N = _X.size() - _P;
if (_M < _N)
_N = _M;
if (this == &_X)
erase((size_type)(_P + _N)), erase(0, _P);
else if (0 < _N && _N == _X.size()
&& _Refcnt(_X.c_str()) < _FROZEN - 1
&& allocator == _X.allocator)
{_Tidy(true);
_Ptr = (_E *)_X.c_str();
_Len = _X.size();
_Res = _X.capacity();
++_Refcnt(_Ptr); }
else if (_Grow(_N, true))
{_Tr::copy(_Ptr, &_X.c_str()[_P], _N);
_Eos(_N); }
return (*this); } 这个成员函数还调用了++_Refcnt(_Ptr); 这个函数,看下他是怎样的 unsigned char& _Refcnt(const _E *_U)
{return (((unsigned char *)_U)[-1]); } 可以发现_Ptr是从-1开始的,_Ptr[-1]保存的是_Ptr的引用计数。
相关阅读 更多 +