Jump to content
  • Advertisement
Sign in to follow this  
Schnozzinkobenstein

C++ return type

This topic is 3156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, so I have a function expecting this argument:
ID3D10Buffer *const *ppVertexBuffers
and I have a function prototype:
ID3D10Buffer* getVertexBuffer();
but it will not take my function as that parameter unless I cast it to:
ID3D10Buffer *const *
I realize it seems pretty obvious what the problem is, but if I have
ID3D10Buffer* frickinVertexBuffer
then I can just pass
&frickinVertexBuffer
as the parameter. Anyone care to explain? Note: I can't make my function
ID3D10Buffer getVertexBuffer();
because ID3D10Buffer is abstract. I'm working in VS2008 in case anyone cares.

Share this post


Link to post
Share on other sites
Advertisement
You are trying to take the address of a temporary returned by that function, which is a bad idea. Functions taking those arguments (which I assume is a D3D10 API function) will often write to the argument (the ID3D10Buffer *const *ppVertexBuffers). If you would pass the return value from the function it would try to write to the temporary return value which could crash, and wouldn't do what you expect. You need to pass the address of a variable that can be updated.

Share this post


Link to post
Share on other sites
Casting it obviously won't help, because D3D10Device::IASetVertexBuffers (I'm guessing this is your mystery function.. providing more information never hurts :]) expects a pointer to a pointer, whereas your getVertexBuffer returns just a pointer.

So just do the obvious thing:


ID3D10Buffer * const pVertexBuffers = getVertexBuffer();
pD3DDevice->IASetVertexBuffers(/* ... */, &pVertexBuffers, /* ... */);

Share this post


Link to post
Share on other sites
Quote:
Original post by Schnozzinkobenstein
Okay, so I have a function expecting this argument: ID3D10Buffer *const *ppVertexBuffers


That reads "A pointer to a (const pointer) to an ID3D10Buffer". The type is read backwards. If you wanted "a (const pointer) to a pointer to an ID3D10Buffer", that is ID3D10Buffer** const.

Since you have a non-const pointer being passed in (even though that pointer points to a const pointer), you can't pass the address of a temporary.

Quote:
Note: I can't make my function
ID3D10Buffer getVertexBuffer();
because ID3D10Buffer is abstract.


No, but you could at least use smart pointers. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!