Smart pointer with the old code.

M

ManicQin

How to use smart pointers with a function which takes raw pointer

Do you mean something like

void foo(char* arg1)
{
auto_ptr<char>(arg1)
//Do something
}
 
A

AnonMail2005

How to use smart pointers with a function which takes raw pointer

Just pass the raw pointer to the function (e.g. use get () or whatever
memeber function your particular smart pointer has to retrieve the raw
pointer it holds)

A well designed stand alone function should not delete the pointer (it
didn't allocated the object did it?) and since the object exists
before the function was called (and will after the function is called)
there is no need to manage it's memory.
 
J

James Kanze

Just pass the raw pointer to the function (e.g. use get () or whatever
memeber function your particular smart pointer has to retrieve the raw
pointer it holds)
A well designed stand alone function should not delete the
pointer (it didn't allocated the object did it?) and since the
object exists before the function was called (and will after
the function is called) there is no need to manage it's
memory.

That depends on what the function is supposed to do. If the
function's role involves deleting the object, it should delete
it. Otherwise, you'll end up with a lot of memory leaks.
 
A

AnonMail2005

That depends on what the function is supposed to do.  If the
function's role involves deleting the object, it should delete
it.  Otherwise, you'll end up with a lot of memory leaks.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

You are correct. But I used the term "well designed". It strikes me
as not a very good design to have smart pointers (in presumably new
code)
call a function (presumably in old/legacy code) that deletes an
object.
I assumed the OP had or was at least striving for good design.
 

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

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top