Skip to content

Commit

Permalink
Update ex14_07.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooophy committed Jun 28, 2015
1 parent 7bc07ff commit 6d2f3ca
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ch14/ex14_07.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,52 @@
std::pair<char*, char*>
String::alloc_n_copy(const char *b, const char *e)
{
auto str = alloc.allocate(e-b);
return{ str, std::uninitialized_copy(b, e, str)};
auto str = alloc.allocate(e - b);
return{ str, std::uninitialized_copy(b, e, str) };
}

void String::range_initializer(const char *first, const char *last)
{
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
end = newstr.second;
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
end = newstr.second;
}

String::String(const char *s)
{
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
}

String::String(const String& rhs)
{
range_initializer(rhs.elements, rhs.end);
range_initializer(rhs.elements, rhs.end);
std::cout << "copy constructor" << std::endl;
}

void String::free()
{
if (elements) {
std::for_each(elements, end, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, end - elements);
}
if (elements) {
std::for_each(elements, end, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, end - elements);
}
}

String::~String()
{
free();
free();
}

String& String::operator = (const String &rhs)
{
auto newstr = alloc_n_copy(rhs.elements, rhs.end);
free();
elements = newstr.first;
end = newstr.second;
auto newstr = alloc_n_copy(rhs.elements, rhs.end);
free();
elements = newstr.first;
end = newstr.second;
std::cout << "copy-assignment" << std::endl;
return *this;
return *this;
}

std::ostream& operator<<(std::ostream &os, const String &s)
Expand Down

0 comments on commit 6d2f3ca

Please sign in to comment.