- 论坛徽章:
- 1
|
XSTRING 中关于string赋值的实现:
_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); }
可见在VC中是共用了缓冲区,而且对缓冲区记录了引用次数(reference count),当对缓冲进行写操作,如果Refcnt>1,则会重新分配,也就是写时复制。
[ 本帖最后由 yuxh 于 2006-8-4 09:03 编辑 ] |
|