Question

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?

Was it helpful?

Solution

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>.

OTHER TIPS

T* get() const;

get returns its pointer by value, so what you were doing was taking the address of a temporary (technically referred to as an rvalue), which isn't allowed.

To fix this, you should store that value within a variable:

INuiSensor* ptr = uni.get();

hr = NuiCreateSensorByIndex(i, &ptr);

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top