Re: Virtual Ctor Idiom and auto_ptr
Terry G wrote:
In the FAQ, the virtual constructor idioms, create() and clone(), use raw
pointer return values.
Wouldn't it be better if these returned auto_ptr's instead?
Not that I can see.
But then covariant return types can't be exploited.
Here's some sample code.
Note that the use of clone() in main() is difficult to upcast.
I suppose that if one is using clone() then polymorphic behavior is
probably
desired, so the upcast typically isn't necessary.
It seems to be that if clone() is going to force an auto_ptr upon its
callers, that the auto_ptr returned should - at the very least - be the
right type:
class Derived
{
public:
std::auto_ptr<Derived> clone() const;
...
};
After all, since Base::clone() is not declared virtual,
Derived::clone() can return whatever it likes.
Of course the caller is still left holding the auto_ptr. So the
question is: what is wrong with letting the caller decide what they
want to do with the returned pointer? The caller can assign it to an
auto_ptr on their own, it is not anything that Base or Derived must do
for them. Nor is it anything that Base or Derived should do - if it is
against the caller's wishes.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]