CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2005
    Posts
    58

    about "auto_ptr"

    Code:
    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?
    Last edited by greghua; March 4th, 2005 at 09:07 PM.

  2. #2
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: about "auto_ptr"

    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.
    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();
        }
    Insert entertaining phrase here

  3. #3
    Join Date
    Dec 2004
    Posts
    85

    Re: about "auto_ptr"

    If you write your own auto_ptr make sure that control of the pointer is transfered during assignment

    Here's an example:
    Code:
    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

  4. #4
    Join Date
    Jan 2005
    Posts
    58

    Re: about "auto_ptr"

    Thank you, i will tag the code next time.

    Quote Originally Posted by wien
    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.
    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();
        }

  5. #5
    Join Date
    Nov 2003
    Posts
    1,405

    Re: about "auto_ptr"

    Quote Originally Posted by greghua
    [ 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.
    Last edited by _uj; March 5th, 2005 at 12:03 AM.

  6. #6
    Join Date
    May 2004
    Posts
    340

    Re: about "auto_ptr"

    and i will add in ,why not get something from the More Effective C++ by Scott Meyers in the Item 28?
    regards,
    jolley

  7. #7
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: about "auto_ptr"

    Wow, which compiler are you using that doesn't support auto_ptr? A common mistake is to forget to include the memory header file:
    Code:
    #include <memory>
    which is where auto_ptr lives.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  8. #8
    Join Date
    May 2004
    Posts
    340

    Re: about "auto_ptr"

    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 :
    Code:
    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

  9. #9
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: about "auto_ptr"

    Quote Originally Posted by jolley
    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:
    Code:
    #include "jolleys_auto_ptr.h"
    
    int main()
        {
        auto_ptr<int> one(new int);
        auto_ptr<int> two(one);
        } // Boom, you're dead!
    Last edited by wien; March 7th, 2005 at 02:19 AM.
    Insert entertaining phrase here

  10. #10
    Join Date
    Sep 2004
    Posts
    519

    Re: about "auto_ptr"

    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.

  11. #11
    Join Date
    May 2004
    Posts
    340

    Re: about "auto_ptr"

    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 i quote from the Meyers :
    Code:
    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

  12. #12
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: about "auto_ptr"

    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
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured