#include <iostream>
#include <memory>
#include <string>
using namespace std;
void string_example() {
auto a = string("original value");
auto b = a; // true copy by copying the value
a = string("new value");
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
}
void shared_ptr_example() {
auto a = make_shared<string>("original value");
auto b = a; // not a copy, just and alias
*a = string("new value"); // and this gonna hurt b
cout << "a = " << *a << endl;
cout << "b = " << *b << endl;
cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
}
void shared_ptr_to_const_example() {
auto a = make_shared<const string>("original value");
auto b = a;
//*a = string("new value"); // <-- now won't compile
a = make_shared<const string>("new value");
cout << "a = " << *a << endl;
cout << "b = " << *b << endl;
cout << boolalpha << "&a == &b ? " << (&a==&b) << endl;
}
int main() {
cout << "--------------" << endl;
cout << "string example" << endl;
string_example();
cout << "------------------" << endl;
cout << "shared_ptr example" << endl;
shared_ptr_example();
cout << "---------------------------" << endl;
cout << "shared_ptr to const example" << endl;
shared_ptr_to_const_example();
}