0

I'm trying to pass a smart pointer as a parameter of a function which takes a pointer of a pointer. I was wondering if there is any proper solution for that.

foo(Class** input)
{
   // Do something
}

myClass = std::make_Unique<Class>();

foo(&myClass.get())
1
  • Why would you want to do that? Manipulating the pointer that a smart pointer manages sounds a bit dangerous. Commented Sep 12, 2018 at 10:38

1 Answer 1

2

That won't work, and it's a good thing that it doesn't.

There are roughly two reasons why you need to pass a Foo**. It could be a function that wants a 2D array (array of pointers to arrays), or the function has a Foo*as output (should have used a Foo*& then). In your case, it appears that you're dealing with the array case, since the argument is called input.

Sign up to request clarification or add additional context in comments.

8 Comments

I'd be reluctant to call T** a 2D array. Array of pointers has a different memory layout than a 2D array, i.e. T[N][M]. The difference is quite substantial.
Thanks for your response. It's actually a return value of the function foo. but I can't change the signature.
@Mohsen67 Why would you want to pass a pointer to a function that wants to return a pointer?
What else can I do? either pass a raw pointer or I thought it's better to use smart pointer
@Mohsen67: Well , you are right, but the signature isn't right.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.