Click to See Complete Forum and Search --> : about "auto_ptr"
greghua
March 4th, 2005, 05:18 PM
class A
{
public:
A(int a,int b):a1(a),a2(b){}
void output(){cout<<"a1="<<a1<<";a2="<<a2<<endl;}
private:
int a1;
int a2;
};
template<class T>
class auto_ptr {
public:
auto_ptr(T *p = 0): ptr(p) {}
~auto_ptr() { delete ptr;}
T* operator ->(){return ptr;}
private:
T *ptr;
};
class B
{
public:
B(int a,int b):ca(new A(a,b)){}
void output() {ca->output();}
private:
const auto_ptr<A> ca;
};
void main()
{
B b(3,4);
b.output();
}
In B, its member data is pointer which point to a class A. with the use of auto_ptr, the point will be delete automatically after object b goes out of range. my compiler doesn't support auto_ptr in standard libary, so I write it by myself.
my quest is, the program can not be compiled, but if I get of the "const" before auto_ptr<A>, it can be compiled and run right. why?
wien
March 4th, 2005, 06:37 PM
You're calling a non-const function on a const object. That's why it fails. make the -> operator const. Also, the main function returns an int.
And please use the code tags the next time you post code.#include <iostream>
using namespace std;
class A
{
public:
A(int a,int b):a1(a),a2(b){}
void output(){cout<<"a1="<<a1<<";a2="<<a2<<endl;}
private:
int a1;
int a2;
};
template<class T>
class auto_ptr
{
public:
auto_ptr(T *p = 0): ptr(p) {}
~auto_ptr() { delete ptr;}
T* operator ->()const{return ptr;}
private:
T *ptr;
};
class B
{
public:
B(int a,int b):ca(new A(a,b)){}
void output() {ca->output();}
private:
const auto_ptr<A> ca;
};
int main()
{
B b(3,4);
b.output();
}
Ben_Phillips
March 4th, 2005, 07:49 PM
If you write your own auto_ptr make sure that control of the pointer is transfered during assignment
Here's an example:
auto_ptr<int> a(someIntPtr);
auto_ptr<int> b;
b = a;
If both a and b point to the same variable then the variable will be "double deleted" (a's destructor will delete it and b's constructor will delete). During this sort of assignment you would want b to be the sole auto_ptr pointing to that pointer (for example set a as pointing to NULL afterwards).
Better yet just download the smart pointers from boost. They are fully implemented and can be used in containers (while auto_ptrs cannot).
http://boost.org/libs/smart_ptr/smart_ptr.htm
greghua
March 4th, 2005, 08:08 PM
Thank you, i will tag the code next time.
You're calling a non-const function on a const object. That's why it fails. make the -> operator const. Also, the main function returns an int.
And please use the code tags the next time you post code.#include <iostream>
using namespace std;
class A
{
public:
A(int a,int b):a1(a),a2(b){}
void output(){cout<<"a1="<<a1<<";a2="<<a2<<endl;}
private:
int a1;
int a2;
};
template<class T>
class auto_ptr
{
public:
auto_ptr(T *p = 0): ptr(p) {}
~auto_ptr() { delete ptr;}
T* operator ->()const{return ptr;}
private:
T *ptr;
};
class B
{
public:
B(int a,int b):ca(new A(a,b)){}
void output() {ca->output();}
private:
const auto_ptr<A> ca;
};
int main()
{
B b(3,4);
b.output();
}
_uj
March 4th, 2005, 10:59 PM
[ my compiler doesn't support auto_ptr in standard libary, so I write it by myself.
Implementing smart pointers is a delicate matter. Even experts fail. This is a portable standard library you can use instead,
http://www.stlport.com/
Or even better. Use the shared_ptr of Boost that has been suggested. It has a much broader usage. It can be used in STL containers for example. It's up for inclusion in the standard library.
jolley
March 5th, 2005, 12:18 AM
and i will add in ,why not get something from the More Effective C++ by Scott Meyers in the Item 28?
regards,
jolley
cma
March 5th, 2005, 12:08 PM
Wow, which compiler are you using that doesn't support auto_ptr? A common mistake is to forget to include the memory header file: #include <memory> which is where auto_ptr lives.
jolley
March 7th, 2005, 12:27 AM
yeah i agree with u all ,but the auto_ptr should need something if it can run more normally.as for me, i am reading the more effective c++ now ,so Meyers mentions that the auto_ptr u list below is not so perfect as expected.so i would rather :
template<class T>
class auto_ptr
{
public:
explicit auto_ptr(T*p=0),ptr(p){}
~auto_ptr(){delete ptr;}
T*operator->()const
{
return ptr;
}
auto_ptr(auto_ptr& autoptrObject)//copy constructor
{
ptr=autoptrObject->ptr;
}
T&operator=(auto_ptr& autoptrObject) //assignment operator
{
if(this!=autoptrObject.ptr) delete ptr;
ptr=autoptrObject->ptr;
return *this;
}
T&operator*()const
{
return *ptr;
}
private:
T*ptr;
};
REgards,
jolley
wien
March 7th, 2005, 01:17 AM
yeah i agree with u all ,but the auto_ptr should need something if it can run more normally.as for me, i am reading the more effective c++ now ,so Meyers mentions that the auto_ptr u list below is not so perfect as expected.so i would rather :
<snip>Not that I understand a word of what you are trying to get across, but:#include "jolleys_auto_ptr.h"
int main()
{
auto_ptr<int> one(new int);
auto_ptr<int> two(one);
} // Boom, you're dead!
marten_range
March 7th, 2005, 02:41 AM
If you wish to make your own scope guard or smart pointer that is fine IMO but don't call it auto_ptr unless it correctly mimics every aspect of std::auto_ptr. It would be terribly confusing for a programmer used to std::auto_ptr.
jolley
March 7th, 2005, 08:53 AM
yeah i agree,but i do think that the auto_ptr is appetising,i have no reason that i won't earn it.in order to put an end to the auto_ptr,and this ti me :cool: i quote from the Meyers :
template<class T>
class auto_ptr
{
explicit auto_ptr(T*p=0):pointee(p){}
template<class U>
auto_ptr(auto_ptr<U>&rhs):pointee(rhs.release()){}
~auto_ptr(){delete pointee;}
template<class U>
auto_ptr<T>& operator=(auto_ptr<U>&rhs)
{
if(this!=&rhs) reset(rhs.release());
return *this;
}
T&operator*()const{return *pointee;}
T*operator->()const{return pointee;}
T*get()const{return pointee;}
T*release()
{
T*oldPointee=pointee;
pointee=0;
return oldpointee;
}
void reset(T*p=0)
{
if(pointee!=p)
{
delete pointee;
pointee=p;
}
}
private:
T*pointee;
template<class U>friend class auto_ptr<U>;
};
guy,it is time to sleep now ,bye,bye :)
Axter
March 8th, 2005, 01:12 PM
You might want to consider using a smart reference pointer instead.
Check out the auto_ref_ptr class in the following link:
http://code.axter.com/auto_ref_ptr.h
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.