1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <iostream> #include <string>
class Entity { private: std::string name; public: Entity() : name("Unknown") {} Entity(const std::string& n) : name(n) {}
const std::string& GetName() const { return name; } };
int main() { Entity e("qinhan"); std::cout << e.GetName() << std::endl;
Entity* e1; { Entity test("aaa"); e1 = &test; std::cout << test.GetName() << std::endl; }
std::cout << e1->GetName() << std::endl;
{ Entity* test = new Entity("asdf"); e1 = test; std::cout << test->GetName() << std::endl; }
std::cout << e1->GetName() << std::endl;
delete(e1); return 0; }
|