标签:小结 size 比较 main ace 没有 结果 turn 标识
#include <iostream> #include <string> using namespace std; class Test { int i; public: Test(int _val) { this->i = _val; cout << "Test(int _val)" << endl; } ~Test() { cout << "~Test()" << endl; } int get_value() { return i; } }; class Pointer { Test* mp; public: Pointer(Test *p = NULL) { mp = p; } //执行深拷贝 Pointer(const Pointer&obj) { mp = obj.mp; //剥夺初始化对象的只读属性 const_cast<Pointer&>(obj).mp = NULL; } //重载赋值操作符 Pointer& operator = (const Pointer& obj) { if (this!= &obj) { delete mp; mp = obj.mp; const_cast<Pointer&>(obj).mp = NULL; } return *this; } Test* operator ->() { return mp; } Test& operator *() { return *mp; } ~Pointer() { delete mp; } //如果mp等于NULL,返回true:1 bool isNULL() { return (mp==NULL); } }; int main() { cout << "Hello World!\n" <<endl; //使用类对象来代替指针,在变量p(对象)生命周期结束时 //执行析构函数,释放变量mp Pointer p = new Test(6); cout << p->get_value() << endl; Pointer p1 = p; cout << p.isNULL() << endl; cout << p1->get_value() << endl; } 运行结果: Hello World! Test(int _val) 6 1 6 ~Test()
标签:小结 size 比较 main ace 没有 结果 turn 标识
原文地址:https://www.cnblogs.com/chengeputongren/p/12234826.html