Link to home
Start Free TrialLog in
Avatar of Vlearns
Vlearns

asked on

auto_ptr

Folder *imf = (*auto_mbox)->get_select_folder();

how do i use a auto_ptr to represent imf?

i have tried

auto_ptr<Folder> imf (new IMAP_Folder((*auto_mbox)->get_select_folder()));

but that does not work

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

>> but that does not work

define "does not work".

does get_select_folder() return a heap allocated pointer created using the new operator? If not auto_ptr cannot be used!

In your second example, assuming it does, you're trying to use a pointer to initialise the constructor of a IMAP_Folder object. Is this correct?

Did you try this?

std::auto_ptr imf ( (*auto_mbox)->get_select_folder() );
Avatar of Vlearns
Vlearns

ASKER

heres tehd eclaration of auto_mbox

auto_ptr<MailboxAutoOpener> auto_mbox(new MailboxAutoOpener(_session.mb
ox()));
That doesn't really help much :)

Please see my previous post where I show how to use auto_ptr based upon my understanding of the type returned but also the importance of making sure the pointer returned is suitable to be managed by an auto_ptr.
Avatar of Vlearns

ASKER

Folder* get_select_folder(bool update = false)
heres what you need, is it ok to use auto_ptr?
>> heres what you need, is it ok to use auto_ptr?
Again, I can't answer that since all that tells me is it returns a pointer but that doesn't tell me if the memory the pointer references was created using new.

What I can tell you is that syntactically the example I show you above will work. What I can't tell you is if this will be safe. You need to check to see how the memory the pointer references is allocated. If this is a 3rd partly library you'll need to refer to the documentation.
Avatar of Vlearns

ASKER

thanks gazillion, i will check it out

bool  Command::fne(
    const char* tag,
    const char* name,
     Folder *imf
if i wanted to sent the auto pointer to this function  (assuming its correct) what would be the equibvalent signature

sorry i am new to auto_ptrs...
The canonical form for using auto_ptr is always this...


auto_ptr< TYPE > varname

So, if you wish to define an auto_ptr of type Folder the form will be this

auto_ptr< Folder> varname

To get from a pointer to an auto_ptr just lose the * and move the type into the angled brackets.

Folder *

Lose the *

Folder

Put type in angled brackets

< Folder >

Prefix with auto_ptr

auto_ptr< Folder >

Does that make sense?
Avatar of Vlearns

ASKER

thanks that helps

i have changed the signatures for the function,

bool  Command::fne(
    const char* tag,
    const char* name,
     auto_ptr<Folder>


before changing the function, imf was passed as follows:


bool  Command::fne(
    tag,
    name,
     imf)

now that i have chanhed teh interfaces, the compiler complains that i am passing
 auto_ptr<Folder> in my call when the signature is

auto_ptr<Folder> &





Can you show me the code please?
Avatar of Vlearns

ASKER

actually let me try this out, i could be wrong
Avatar of Vlearns

ASKER

although i do understand how instances of *imf are to be replaced

i have places where  imf->uidnext()

how would use of imf like above (without the *) change after using auto_ptrs
It doesn't. The whole point is that from an interface point of view auto_ptr is the same as a normal pointer. It respects all the normal pointer dereference and indirection operators that you would normally use on a pointer.

Folder * pf1 = new Folder;
auto_ptr pf2(new Folder);

pf1->uidnext() ;
pf2->uidnext() ;

Folder const & r1 = *pf1;
Folder const & r2 = *pf2;
Avatar of Vlearns

ASKER

what is the imapct if teh auto_ptr above is created for a memory created using malloc and not new?...what re the safety issue?
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vlearns

ASKER

thanks a million!!!!