T
Tobias Langner
I overloaded the operator= - but in the program, if the line p3=p1 gets
executed, the program doesn't jump to the method.
The class definition and the test code.
template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T, OwnershipPolicy>
&);
~SmartPtr();
T& operator*() const;
T* operator->() const;
private:
void deleteObject();
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;
};
//The Testcode:
bool testSmartPtr() {
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(1));
SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(20));
SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
RefCount>(*p2);
std::cout<<"p1=p3\n";
p3=p1;
std::cout<<"delete p1\n";
delete p1;
std::cout<<"delete p2\n";
delete p2;
std::cout<<"delete p3\n";
delete p3;
return true;
}
executed, the program doesn't jump to the method.
The class definition and the test code.
template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T, OwnershipPolicy>
&);
~SmartPtr();
T& operator*() const;
T* operator->() const;
private:
void deleteObject();
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;
};
//The Testcode:
bool testSmartPtr() {
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(1));
SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(20));
SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
RefCount>(*p2);
std::cout<<"p1=p3\n";
p3=p1;
std::cout<<"delete p1\n";
delete p1;
std::cout<<"delete p2\n";
delete p2;
std::cout<<"delete p3\n";
delete p3;
return true;
}