#include <cstring>
#include <iostream>
#include <memory>
class String {
public:
String(const char* s) : data_(strdup(s)) {}
~String() { free(const_cast<char*>(data_)); }
// Строки в Java immutable, имитирующие им C++ строки - тоже.
String operator=(String&&) = delete;
String operator=(const String&) = delete;
friend std::ostream& operator<<(std::ostream& o,
const String& s) {
o << s.data_;
return o;
}
private:
const char* data_;
};
int main() {
auto foo = std::make_shared<String>("foo");
auto bar = std::make_shared<String>("bar");
std::swap(foo, bar);
// foo приняло значение "bar", а bar" приняло значение "foo".
std::cout << "foo: " << (*foo) << "\n";
std::cout << "bar: " << (*bar) << "\n";
std::cin.get();
}
I2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPG1lbW9yeT4KCmNsYXNzIFN0cmluZyB7CiBwdWJsaWM6CiAgU3RyaW5nKGNvbnN0IGNoYXIqIHMpIDogZGF0YV8oc3RyZHVwKHMpKSB7fQogIH5TdHJpbmcoKSB7IGZyZWUoY29uc3RfY2FzdDxjaGFyKj4oZGF0YV8pKTsgfQogIC8vINCh0YLRgNC+0LrQuCDQsiBKYXZhIGltbXV0YWJsZSwg0LjQvNC40YLQuNGA0YPRjtGJ0LjQtSDQuNC8IEMrKyDRgdGC0YDQvtC60LggLSDRgtC+0LbQtS4KICBTdHJpbmcgb3BlcmF0b3I9KFN0cmluZyYmKSA9IGRlbGV0ZTsKICBTdHJpbmcgb3BlcmF0b3I9KGNvbnN0IFN0cmluZyYpID0gZGVsZXRlOwogIAogIGZyaWVuZCBzdGQ6Om9zdHJlYW0mIG9wZXJhdG9yPDwoc3RkOjpvc3RyZWFtJiBvLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29uc3QgU3RyaW5nJiBzKSB7CiAgICBvIDw8IHMuZGF0YV87CiAgICByZXR1cm4gbzsKICB9CiBwcml2YXRlOgogIGNvbnN0IGNoYXIqIGRhdGFfOwp9OwoKCmludCBtYWluKCkgewogIGF1dG8gZm9vID0gc3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