#include <iostream> using namespace std; class Test { public: Test() { cout << "Test()" << endl; } ~Test() { cout << "~Test()" << endl; } void Display() { cout << "This is the Test()" << endl; } }; class TestController { public: TestController() { this->test_ = new Test(); } ~TestController() { delete this->test_; } Test* operator ->() { return this->test_; } private: Test* test_; }; int main() { TestController tController; tController->Display(); return 0; }
通过包装一个类,重载指针运算符,完成对一个在堆上开辟空间的释放