refcountedstring/Char.cpp

22 lines
572 B
C++
Raw Normal View History

#include "Char.h"
Char::operator char() const
{ return _string._str->operator[](_index); }
2016-10-29 09:39:18 +00:00
// copy-on-write magic happens here
// provides strong exception guarantee
Char& Char::operator=(char other)
{
auto data = new char[_string.size() + 1]; // space for old string & null-terminator
std::strcpy(data, _string.c_str());
data[_index] = other; // set the desired char
_string._str->operator--(); // release old StringValue
_string._str = new StringValue(data); // make a new one
return *this;
}