-
August 25th, 2010, 07:05 PM
#1
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.
-
August 25th, 2010, 08:44 PM
#2
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.
-
August 25th, 2010, 08:46 PM
#3
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.
-
August 26th, 2010, 12:34 AM
#4
Re: How to make auto_ptr work polymorphically?
Originally Posted by Philip Nicoletti
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.
-
August 26th, 2010, 12:36 AM
#5
Re: How to make auto_ptr work polymorphically?
Originally Posted by Lindley
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.
-
August 26th, 2010, 03:42 AM
#6
Re: How to make auto_ptr work polymorphically?
Originally Posted by LarryChen
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.
-
August 26th, 2010, 04:17 AM
#7
Re: How to make auto_ptr work polymorphically?
Originally Posted by Lindley
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
-
August 26th, 2010, 04:51 AM
#8
Re: How to make auto_ptr work polymorphically?
Originally Posted by D_Drmmr
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|