A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

48 lines
751 B

#include <string>
#include <memory>
#include <iostream>
using std::string, std::unique_ptr,
std::shared_ptr, std::make_unique;
class BadRef {
string &name;
public:
BadRef(string &name) : name(name) {}
void set_name(string &n) {
name = n;
}
};
class GoodRef {
string *name;
public:
/*
* After calling, name is owned.
*/
GoodRef(string *n) : name(n) {}
void print() {
std::cout << "My name is " << *name << std::endl;
}
};
int main() {
string my_name = "Zed";
string your_name = "Alex";
string &ref_test = my_name;
string *ptr_name = new string("Pointer");
ref_test = your_name;
auto br = BadRef(my_name);
br.set_name(your_name);
auto gr = GoodRef(ptr_name);
gr.print();
return 0;
}