How to make auto_ptr work polymorphically?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: How to make auto_ptr work polymorphically?

  1. #1
    Join Date
    Jul 2005
    Posts
    940

    How to make auto_ptr work polymorphically?

    Check the following code,
    Code:
    class B
    {
    public:
    	virtual void display()
    	{
    		cout<<"B"<<endl;
    	}
    };
    
    class D : public B
    {
    public:
    	void display()
    	{
    		cout<<"D"<<endl;
    	}
    };
    
    void bar(auto_ptr<B> ptr)
    {
    	ptr->display();
    }
    
    int main(int argc, char* argv[])
    {
        auto_ptr<D> ptr(new D);
    
    	bar(ptr);
    	return 0;
    }
    Actually it won't compile. I wonder how to make it work? Thanks.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,630

    Re: How to make auto_ptr work polymorphically?

    Code:
    auto_ptr<B> ptr(new D);
    Note : passing atuo_ptrs are problematic because of the ownership
    of the pointer when copied.

    After calling the function, do the following:

    Code:
    bar(ptr);
    
    if (ptr.get() == NULL)  
    {
       cout << "null\n"; 
    }
    Also, add a destructor to the classes, printing out a message, and see
    when the pointer gets deleted.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: How to make auto_ptr work polymorphically?

    As with all templates, a class<A> is not related to a class<B>, no matter whether A and B are related or not.

    However, you could write:
    Code:
    auto_ptr<B> ptr(new D);
    and that would be fine.

    Note, however, that the D object will be destroyed after the call to bar() since you're passing the auto_ptr by value. This is unintuitive, which is why auto_ptr has been deprecated in favor of unique_ptr, which has the same behavior but is explicitly only movable, not copyable.

  4. #4
    Join Date
    Jul 2005
    Posts
    940

    Re: How to make auto_ptr work polymorphically?

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    auto_ptr<B> ptr(new D);
    Note : passing atuo_ptrs are problematic because of the ownership
    of the pointer when copied.

    After calling the function, do the following:

    Code:
    bar(ptr);
    
    if (ptr.get() == NULL)  
    {
       cout << "null\n"; 
    }
    Also, add a destructor to the classes, printing out a message, and see
    when the pointer gets deleted.
    I tried to call ptr.get() but it doesn't return NULL after passing to the function bar. I expect it to be NULL but I don't know why it isn't. Thanks.

  5. #5
    Join Date
    Jul 2005
    Posts
    940

    Re: How to make auto_ptr work polymorphically?

    Quote Originally Posted by Lindley View Post
    As with all templates, a class<A> is not related to a class<B>, no matter whether A and B are related or not.

    However, you could write:
    Code:
    auto_ptr<B> ptr(new D);
    and that would be fine.
    That is a good idea. Thanks.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,630

    Re: How to make auto_ptr work polymorphically?

    Quote Originally Posted by LarryChen View Post
    I tried to call ptr.get() but it doesn't return NULL after passing to the function bar. I expect it to be NULL but I don't know why it isn't. Thanks.
    What compiler/version are you using ? auto_ptr in VC++ version 6
    had bugs in the implementation that was shipped.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: How to make auto_ptr work polymorphically?

    Quote Originally Posted by Lindley View Post
    As with all templates, a class<A> is not related to a class<B>, no matter whether A and B are related or not.
    But if you write your own template class you can make class<A> convertible to class<B> by providing an overloaded constructor, as is done for shared_ptr.

    From http://www.boost.org/doc/libs/1_44_0...shared_ptr.htm
    shared_ptr<T> can be implicitly converted to shared_ptr<U> whenever T* can be implicitly converted to U*. In particular, shared_ptr<T> is implicitly convertible to shared_ptr<T const>, to shared_ptr<U> where U is an accessible base of T, and to shared_ptr<void>.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  8. #8
    Join Date
    Oct 2008
    Posts
    1,153

    Re: How to make auto_ptr work polymorphically?

    Quote Originally Posted by D_Drmmr View Post
    But if you write your own template class you can make class<A> convertible to class<B> by providing an overloaded constructor, as is done for shared_ptr.

    From http://www.boost.org/doc/libs/1_44_0...shared_ptr.htm
    in principle auto_ptr can convert from/to auto_ptr's whose base pointers are convertible;

    the problem in the OP code is that auto_ptr exposes both a template copy ctor taking any auto_ptr<T> by non const reference and a template conversion operator to any auto_ptr<T> which give an ambiguous overload resolution error in the bar argument copy-initialization. As in

    Code:
    struct A;
    
    struct B { B( A& ); }; // if it were "B( const A& );" it would compile
    
    struct A { operator B(); };
    
    void bar( B );
    
    int main()
    {
    	A a;
    	bar(a);
    }
    indeed, an alternative to "auto_ptr<B> ptr = auto_ptr<B>( new D() )" is

    Code:
    int main()
    {
    	auto_ptr<D> ptr(new D);
    
    	bar( auto_ptr<B>(ptr) );
    }

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center