Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing smart pointer to a function taking reference to a pointer parameter

How can I pass smart ptr to a function taking reference to a pointer as a parameter?

smart_ptr<T> val; // I have this smart pointer

// And I want to pass it to this function, so that this function will fill the smart pointer with proper value
void Foo(T*& sth)
{
    sth = memoryAddress;
}

EDIT Now I get it. Thanks guys for all the answers!

like image 485
user2032932 Avatar asked Sep 03 '25 15:09

user2032932


1 Answers

The simple answer is that you can't. While the smart pointer almost certainly contains a T* somewhere internally, smart pointers enforce all sorts of invariants, many of which could be broken if you could change this pointer without passing through the user interface. The only solution is to call the function with a raw pointer, and then use the raw pointer to initialize the smart pointer, provided that you're sure that the pointer you get meets the requirements of the smart pointer (e.g. allocated by the new operator).

like image 82
James Kanze Avatar answered Sep 05 '25 04:09

James Kanze