function return of type auto_ptr

S

softwareEngineer

Hi all,
i've a question about auto_ptr. It's correct returning an auto_ptr from function ?
Example :

<CODE>
class BookShops
{
...
std::auto_ptr<BOOK> GetBook (int book_id);
};
</CODE>

This is only a dummy example but i wold know if this is standard and if it is a
good design practice.

many thanks.
 
H

Howard Hinnant

Hi all,
i've a question about auto_ptr. It's correct returning an auto_ptr from function ?
Example :

<CODE>
class BookShops
{
  ...
    std::auto_ptr<BOOK> GetBook (int book_id);};

</CODE>

This is only a dummy example but i wold know if this is standard and if it is a
good design practice.

many thanks.

If you're compiler supports it, you may want to return
std::unique_ptr<BOOK> instead. This is a new replacement for auto_ptr
in C++11. std::auto_ptr is deprecated in C++11. However if your
compiler doesn't yet support unique_ptr, don't sweat it. auto_ptr
will work fine for you.
 
G

gwowen

This article is by Herb Sutter. ;-)

It's widely known they're the same person, using two identities for
tax reasons... ;)

[Obviously, yeah, you're right. My point still holds, I think ...]
 
A

Alf P. Steinbach /Usenet

* Edek, on 03.06.2011 21:45:
Now, how does that sound in the context that auto_ptr is deprecated?

Like this: use it, but don't rely on anything more than the simple ownership
semantics. In particular, don't rely on automatic conversion to pointer to base
type. This is because the deprecation means that compiler vendors (read:
Microsoft) are unlikely to fix the remaining bugs in their implementations.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top