#include<iostream>
using namespace std;
class shared_ptr
{
public:
int *pointer;
public:
shared_ptr()
{
pointer = new int;
}
~shared_ptr()
{
delete pointer;
}
int operator* ();
int* operator= (shared_ptr&);
};
int shared_ptr:: operator* ()
{
return *(this->pointer);
}
int* shared_ptr:: operator= (shared_ptr& temp)
{
return (temp.pointer);
}
int main()
{
shared_ptr s1;
*(s1.pointer) = 10;
cout << *s1 << endl;
int *k;
k = s1; //error
cout << *k << endl;
}
我正在尝试创建类似智能指针的东西。
尝试重载 operator = 时出现以下错误。
prog.cpp:39:9:错误:无法在 k = s1 分配行的分配中将“shared_ptr”转换为“int*”。我在这里想念什么?