I am trying to use the function NuiCreateSensorByIndex(int,INuiSensor**). I am trying not to use naked pointers, so I did std::unique_ptr<INuiSensor> nui; to make it an unique_ptr.
Now I want to acces this function, so I do the following: hr = NuiCreateSensorByIndex(i, &nui.get());, but this is wrong:
KinectManager.cpp:29: error: C2102: '&' requires l-value
What am I doing wrong and how to fix it?
The compiler is right: although std::unique_ptr<INuiSensor> can be used to point to things, it is not an object a pointer to which is expected by the NuiCreateSensorByIndex(int,INuiSensor**) function. The reason the function wants a pointer to a pointer is that it wants to modify pointer's content by an assignment of this sort:
*ptrToPtr = somePtr;
If compiler let you pass a pointer to std::unique_ptr<INuiSensor>, this assignment would be invalid. That's why you need to create a temporary "naked" pointer, pass it to the function, and then assign the result back to std::unique_ptr<INuiSensor>.
The purpose of unique_ptr is to take control of ownership of resource. The pointer to pointer is to modify the address of the resource, which will change the ownership of the resource. One solution is to release the ownership first, then reclaim it, such as:
auto p = uni.release();
NuiCreateSensorByIndex(0, &p);
uni.reset(p);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With