A Sample auto_ptr implementation
Hi All,
I'm building a sample application that uses a custom auto_ptr
implementation.
The program crashes with the following output:-
Output (Debug Assertion failed)
----------
release called
copy constructor
aap: ptr2= 10
aap: ptr3= 20
aap: ptr3= 10
aap: exiting app
Deleting pointee...10
Deleting pointee...-572662307 (...problem ?)
Code
--------
#include<iostream>
using namespace std;
// MyAutoPtr Interface
template <class T>
class MyAutoPtr
{
public:
explicit MyAutoPtr(T* ptr=0);
~MyAutoPtr();
template <class U>
MyAutoPtr(MyAutoPtr<U> *rhs);
//copy constructor member template, initialize this member pointer
with any other compatible auto_ptr
template <class U>
MyAutoPtr(MyAutoPtr<U>& rhs);
//assignment operator
template <class U>
MyAutoPtr<T>& operator=(MyAutoPtr<U>& rhs);
//relinquish ownership
T* release();
T* get() const;
//reset the auto_ptr, delete pointee, assume ownership of p
void reset(T* ptr=0);
T& operator*() const;
T* operator->() const;
private:
T* pointee;
//template <class U>
//friend class MyAutoPtr<U>;
};
//Implementation
template<typename T>
inline MyAutoPtr<T>::MyAutoPtr(T* ptr):pointee(ptr)
{ }
template <class T>
inline MyAutoPtr<T>::~MyAutoPtr()
{
if(pointee)
{
cout<<"\n\t Deleting pointee..."<<*pointee;
delete pointee;
}
}
template <class T>
template <class U>
inline MyAutoPtr<T>::MyAutoPtr(MyAutoPtr<U>& rhs) :
pointee(rhs.release())
{
cout<<"\n\t copy constructor";
}
template<class T>
template<class U>
MyAutoPtr<T>& MyAutoPtr<T>::operator=(MyAutoPtr<U>& rhs)
{
cout<<"\n\t Inside operator=";
if(this!=&rhs)
{
cout<<"\n\t Inside operator=, calling reset";
reset(rhs.release());
}
return *this;
}
template <class T>
T* MyAutoPtr<T>::get() const
{
return pointee;
}
template <class T>
inline T& MyAutoPtr<T>::operator *() const
{
return *pointee;
}
template<class T>
inline T* MyAutoPtr<T>::operator->() const
{ return pointee; }
template<class T>
T* MyAutoPtr<T>::release()
{
cout<<"\n\t release called";
T* oldp=pointee;
pointee=0;
return oldp;
}
template<class T>
void MyAutoPtr<T>::reset(T* p)
{
cout<<"\n\t reset called";
if(pointee!=p)
{
delete pointee;
//pointee=0;
}
pointee=p;
}
void main()
{
MyAutoPtr<int> intp(new int(10));
MyAutoPtr<int> intp2(intp);
MyAutoPtr<int> intp3(new int(20));
//cout<<"ptr1= "<<*intp;
cout<<"\n\t aap: ptr2= "<<*intp2;
cout<<"\n\t aap: ptr3= "<<*intp3;
intp3 = intp2; //
==============================> 1
cout<<"\n\t aap: ptr3= "<<*intp3;
cout<<"\n\t aap: exiting app";
}
It seems, on debugging, that class's custom operator= is somehow not
been called at 1 and hence a problem while destructing. (since delete
is being called twice on the same pointer)
---------------------------------------------------------------------------------------------------------
Q1. Any problems with the above code ?
Q2. What is causing operator= not to be called and how to fix it ?
Q3. Following statements (in the class interface) cause compiler
errors, if not commented out
private:
T* pointee;
//template <class U>
//friend class MyAutoPtr<U>;
MSDN defines this error as:-
Compiler Error C3772
Error Message
"name" : invalid friend template declaration
It is invalid to declare a friend of a class template specialization.
You cannot declare an explicit or partial specialization of a class
template and in the same statement declare a friend of that
specialization. The name placeholder identifies the invalid
declaration.
To correct this error
-Do not declare a friend of a class template specialization.
-If appropriate for your application, declare a friend of the class
template, or declare a friend of a particular partial or explicit
specialization.
Why such error, if this is not a specialization? or is it a
specialization somehow?
---------------------------------------------------------------------------------------------------------
Any help will be highly appreciated!
Thanks!