Can I pass auto_ptr in this way?

Z

zl2k

hi, there,

Can I pass auto_ptr in the following manner?

auto_ptr<T> obj; // The obj is not initialized and null here, right?
function(obj);
// Do I get the initialized obj here?

void function(auto_ptr<T> obj){// can I pass a null auto_ptr into the
function and get initialized inside?
obj(new T());
}

Thanks a lot.

zl2k
 
A

Alf P. Steinbach /Usenet

* zl2k, on 07.09.2010 05:08:
hi, there,

Can I pass auto_ptr in the following manner?

auto_ptr<T> obj; // The obj is not initialized and null here, right?
function(obj);
// Do I get the initialized obj here?

void function(auto_ptr<T> obj){// can I pass a null auto_ptr into the
function and get initialized inside?
obj(new T());
}

No, for two reasons.

First, regardless of the functionality of std::auto_ptr, when you pass an object
O by value, like foo(O) where foo's formal argument is by value, then the effect
is a copy construction of the formal argument from the actual argument. This
means that foo can't do anything directly with the original actual argument.

Secondly, the syntax

obj( new T() );

as a statement is a function call, and when obj is of class type it means

obj.operator()( new T() )

but std::auto_ptr has no operator(), no function call operator.

What you can do is something like

auto_ptr<T> foo() { return auto_ptr<T>( new T() ); }

//...
auto_ptr<T> o1( foo() );
auto_ptr<T> o2 = foo();


Cheers & hth.,

- Alf
 
G

Goran Pusic

You seem to be looking for

void foo(auto_ptr<T>& p) { p=auto_ptr<T>( new T() ); }

(You also seem to not understand a pass-by-reference, and what are
consequences when you pass auto_ptr by reference; and due to the
nature of auto_ptr, there are additional things to understand when you
pass auto_ptr by ref).

Goran.
 
A

Alf P. Steinbach /Usenet

* Goran Pusic, on 07.09.2010 08:46:
You seem to be looking for

void foo(auto_ptr<T>& p) { p=auto_ptr<T>( new T() ); }

(You also seem to not understand a pass-by-reference, and what are
consequences when you pass auto_ptr by reference; and due to the
nature of auto_ptr, there are additional things to understand when you
pass auto_ptr by ref).

Hi Goran.

Please quote sufficiently from the message that you're responding to, to
establish context.

Also, please post your response to a message M as a follow-up to M.


Thanks in advance,

- Alf
 
D

DDD

* Goran Pusic, on 07.09.2010 08:46:




Hi Goran.

Please quote sufficiently from the message that you're responding to, to
establish context.

Also, please post your response to a message M as a follow-up to M.

Thanks in advance,

- Alf

Maybe just need to know auto_ptr<T> obj is a class obj. It's a auto
variable.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top