1.1 c/c++智能指针

c/c++智能指针

(直接上代码)
template <typename T>
class sharePtr
{
public:
	sharePtr() {
		instance_ = new T();
	}
	~sharePtr() {
		delete instance_;
	}
	
    // 为了像指针一样使用
	T* operator->() {
		return instance_;
	}

private:
	T* instance_;
};

class testSharePtr
{
public:
	testSharePtr() {
		cout << "testSharePtr conStructor" << endl;
	}
	~testSharePtr() {
		cout << "testSharePtr deStructor" << endl;
	}

	void print() {
		cout << "testSharePtr -> print()" << endl;
	}
};

void test_sharePtr(){
    sharePtr<testSharePtr> testPtr;
	testPtr->print();
}

猜你喜欢

转载自blog.csdn.net/weixin_37518595/article/details/83546214