473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

auto_ptr and arguments

Often I have a method which accepts a pointer to some class as an
argument. Usually that pointer is managed by the caller through
std::auto_ptr. I would assume that you could pass the auto_ptr in to
the method call and conversion would automagically take place, but this
does not happen. I end up having to explicitly call auto_ptr.get().

Is there a good way around this?

NR

Jul 19 '05 #1
6 2865
There's no conversion operator that returns a value of type T* (where T is
the element type), so in the first case: no. The only way to get around
this would be to derive a class from this one with a method like:

template <class T>
class MyAutoPtr : public std::auto_ptr<T>
{
public:
operator( _Ty* ) const { return _Ptr; }
}

This is probably flawed syntactically (many times over), but you get the
point.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 19 '05 #2
Noah Roberts wrote:
Often I have a method which accepts a pointer to some class as an
argument. Usually that pointer is managed by the caller through
std::auto_ptr. I would assume that you could pass the auto_ptr in to
the method call and conversion would automagically take place, but this
does not happen. I end up having to explicitly call auto_ptr.get().

Is there a good way around this?


No.

auto_ptr is designed specifically to manage that pointer and it is
trying to make sure you do not inadvertenly put that pointer into
another auto_ptr.

I think it's a bit over the top myself but it's not my call.

I have written a more lax replacement for auto_ptr butI would only
reccomend it to people who know what they're doing with all the hidden
issues that come with it.

You could theoretically do this yourself - create a class that is an
auto_ptr that knows how to convert implicitly.
template <typename T>
class MyNastyAutoPtr : public auto_ptr<T>
{
public:

MyNastyAutoPtr( ....)
... other constructors and assignment mumbo jumbo ...

operator const T* ()
{
return get();
}

};

Jul 19 '05 #3
Noah Roberts <nr******@dontemailme.com> wrote in message news:<3F**************@dontemailme.com>...
Often I have a method which accepts a pointer to some class as an
argument. Usually that pointer is managed by the caller through
std::auto_ptr. I would assume that you could pass the auto_ptr in to
the method call and conversion would automagically take place, but this
does not happen. I end up having to explicitly call auto_ptr.get().
auto_ptr is designed to be used like a pointer, not to be
interchangeable with pointers; conversions either way are explicit. It
should not be possible to accidentally give ownership to auto_ptr, nor
to transfer ownership from auto_ptr to some other entity. (It is with
this in mind that I usually shun auto_ptr altogether, in favor of
boost.org smart pointers, where applicable; auto_ptr's copy semantics
are incorrect in almost every case except when returning a dynamic
object from a function. But since it's the only standard smart pointer
right now, I wouldn't blame anyone for using it exclusively.)
Is there a good way around this?


No, nor should there be. ap.get() is the designated way to get access
to the underlying pointer. If you're insane, you could overload your
functions to accept either a pointer or an auto_ptr; or you could
accept, by value or const reference, a class which offers an implicit
conversion from either. But that's a lot more work than just using
"ap.get()" when you mean "get ap's underlying pointer."

- Shane
Jul 19 '05 #4
Michael Winter wrote:
There's no conversion operator that returns a value of type T* (where T is
the element type), so in the first case: no. The only way to get around
this would be to derive a class from this one with a method like:

template <class T>
class MyAutoPtr : public std::auto_ptr<T>
{
public:
operator( _Ty* ) const { return _Ptr; }


These identifiers are reserved for the implementation. C++ programs are
forbidden to use identifiers beginning with an underscore followed by
either an upper case letter or another underscore. Your standard library
probably does this, but that's because it is part of the implementation
and is required to do so - otherwise, it would be treading on names that
are reserved for *your* use.

Also, when you reply please quote some relevant context from the message
you are replying to. Otherwise we don't know exactly what message you
are replying to, and it may even be a message that we haven't received yet.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5
Gianni Mariani wrote:
Noah Roberts wrote:
Often I have a method which accepts a pointer to some class as an
argument. Usually that pointer is managed by the caller through
std::auto_ptr. I would assume that you could pass the auto_ptr in to
the method call and conversion would automagically take place, but
this does not happen. I end up having to explicitly call auto_ptr.get().

Is there a good way around this?

No.

auto_ptr is designed specifically to manage that pointer and it is
trying to make sure you do not inadvertenly put that pointer into
another auto_ptr.


That actually makes a lot of sense to me now that I think about it.

Thanks,
NR

Jul 19 '05 #6
Noah Roberts <nr******@dontemailme.com> wrote in news:3F6C9090.3010504
@dontemailme.com:
Often I have a method which accepts a pointer to some class as an
argument. Usually that pointer is managed by the caller through
std::auto_ptr. I would assume that you could pass the auto_ptr in to
the method call and conversion would automagically take place, but this
does not happen. I end up having to explicitly call auto_ptr.get().

Is there a good way around this?


Well... the first question I'd have is: do you _really_ understand what
auto_ptr<> does?

and... have your function take in an auto_ptr<>& instead of a naked pointer
(if you want to assume that everyone calling you will get using auto_ptr
<>), or you have to .get() the pointer out of the auto_ptr<>.

Automatically converting the auto_ptr<> to a pointer would make it too easy
to "accidentally" get a copy of the pointer, let the auto_ptr go out of
scope, then use that copy of the pointer (now pointing at deleted
memory....)
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: gg | last post by:
I am getting the following compilation errors with the following program. My compiler is aCC 03.27 on HP-UX11 - #include <iostream> using namespace std; #include <list> #include <memory>...
14
by: Andrew | last post by:
Hello all: After spending some time figuring out auto_ptr class' implementation, I decided to write a small article detailing its use of the auto_ptr_ref proxy class to enable construction and...
2
by: Markus Dehmann | last post by:
I am trying to make a pair with a string and an auto_ptr: #include <iostream> #include <map> using namespace std; int main(){ auto_ptr<intp(new int(3)); make_pair("x",p); }
9
by: dragoncoder | last post by:
Hi all, I am trying to understand the auto_ptr_ref role in the implementation of auto_ptr<>. I read the information on net but still not 100% sure of it. My plan is as follows. 1. To see the...
39
by: Andre Siqueira | last post by:
Hello all, I have a member function like thist: Query(const std::string & id, std::auto_ptr<Modifiermodif = std::auto_ptr<Modifier>()) when a try to instantiate a Query like ...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
5
by: Soumen | last post by:
For one of my project, I want to use auto_ptr. But I'm facing few issues. 1. Say I've Controller class. I don't want to allow user to create object of this class unless all arguments are valid....
7
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the...
17
by: Ankur Arora | last post by:
Hi All, I'm building a sample application that uses a custom auto_ptr implementation. The program crashes with the following output:- Output (Debug Assertion failed) ----------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.