简体   繁体   中英

Arrow Pointer to a function return value in c++

I have been trying to understand a simple c++ code that is using Kinect.h interfaces.

If I have a variable of a certain type (say IColourSourceReader* pColorSource) and in the next piece of code, I am accessing the return of another function of another interface through this pointer (pColorSource):

HRESULT hresult = S_OK;

IColorSourceReader* pColorSource;
[-----code to process this information------]

IColorFrameReader* pFrameReader;
hresult = pColorSource->OpenReader(&pFrameReader);

1) How does the arrow operator function here? (as i understand it is equivalent to pointing to variable belonging to a class/struct a->b is same as (*a).b)

2) Is it assigning the value of Return of the OpenReader function to pColorSource (which is of type HRESULT, as documented in the Kinect SDK refernce documents)

I am a beginner in c++ programming but have a challenging task due to which I am using this piece of code to understand and get a start.

please help even if it sounds too fundamental.

the arrow is dereferencing the pointer object, accessing the member in the class/struct pointed to by the pointer object.

pColorSource->OpenReader(&pFrameReader);

is accessing the member function OpenReader of interface IColorSourceReader, operation on the class instance pointed to by pColorSource.

You use the arrow (->) to access members when you have a pointer to (address of) an object instance, and you use a dot (.) to access members when you have an object reference.

Your code sample is incomplete, but there is enough to answer your question.

Create a variable to to store the return value of the method (function) you want to call:

HRESULT hresult = S_OK;`

Create a pointer to an instance of the class IColorSourceReader:

IColorSourceReader* pColorSource;

Missing, but most likely create a new class of type IColorSourceReader and assign it to the pointer:

[-----code to process this information------]

Create a pointer to a class which will be passed (by reference) as the argument of the method:

IColorFrameReader* pFrameReader;

Call the method OpenReader() of the instance of the class IColorSourceReader pointed to by the pointer pColorSource, taking the address of the pointer pFrameReader as an argument, and assign the result of the function to hresult:

hresult = pColorSource->OpenReader(&pFrameReader);

I assume the address of the pointer pFrameReader is being passed so that the method can assign a value to it, and have the value available to your code afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM