#include <iostream>
using namespace std;
class SimpleCat
{
public:
SimpleCat (int age, int weight);
~SimpleCat() {}
int GetAge() { return itsAge; }
int GetWeight() { return itsWeight; }
private:
int itsAge;
int itsWeight;
};
SimpleCat::SimpleCat(int age, int weight):
itsAge(age), itsWeight(weight) {}
SimpleCat & TheFunction();
int main()
{
SimpleCat & rCat = TheFunction();
int age = rCat.GetAge();
cout << "rCat is " << age << " years old!\n";
cout << "&rCat: " << &rCat << endl;
// How do you get rid of that memory?
SimpleCat * pCat = &rCat;
delete pCat;
// Uh oh, rCat now refers to ??
return 0;
}
SimpleCat &TheFunction()
{
SimpleCat * pFrisky = new SimpleCat(5,9);
cout << "pFrisky: " << pFrisky << endl;
return *pFrisky;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIFNpbXBsZUNhdAogewogcHVibGljOgpTaW1wbGVDYXQgKGludCBhZ2UsIGludCB3ZWlnaHQpOwp+U2ltcGxlQ2F0KCkge30KaW50IEdldEFnZSgpIHsgcmV0dXJuIGl0c0FnZTsgfQogaW50IEdldFdlaWdodCgpIHsgcmV0dXJuIGl0c1dlaWdodDsgfQoKIHByaXZhdGU6CiBpbnQgaXRzQWdlOwogaW50IGl0c1dlaWdodDsKIH07CgogU2ltcGxlQ2F0OjpTaW1wbGVDYXQoaW50IGFnZSwgaW50IHdlaWdodCk6CiBpdHNBZ2UoYWdlKSwgaXRzV2VpZ2h0KHdlaWdodCkge30KCiBTaW1wbGVDYXQgJiBUaGVGdW5jdGlvbigpOwoKIGludCBtYWluKCkKIHsKIFNpbXBsZUNhdCAmIHJDYXQgPSBUaGVGdW5jdGlvbigpOwogaW50IGFnZSA9IHJDYXQuR2V0QWdlKCk7CiBjb3V0IDw8ICJyQ2F0IGlzICIgPDwgYWdlIDw8ICIgeWVhcnMgb2xkIVxuIjsKIGNvdXQgPDwgIiZyQ2F0OiAiIDw8ICZyQ2F0IDw8IGVuZGw7CiAvLy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