Sign in to follow this  
mrheisenberg

Is it safe to use IUnknown* for every class that has a DirectX object in it?

Recommended Posts

mrheisenberg    362

For instance, instead of using ID3D11Buffer* in your mesh class, to use IUnknown or isntead of ID3D11PixelShader* to use IUnknown again.Since they inherit from it, casting and passing to functions that require those types shouldn't be a problem, right?

Share this post


Link to post
Share on other sites
Jason Z    6436

That's right - but by passing a reference/pointer to an IUnknown instance you are limited to only using its interface unless you re-cast it back to the original subclass.  The only interfaces that the IUnknown interface provides are AddRef and Release, so I don't think this would be a very good way to go though.

Share this post


Link to post
Share on other sites
mhagain    13430

This seems fundamentally unsafe to me.  In order to do anything useful with the IUnknown you're talking about having to cast it back to it's original interface, but you don't know what the original interface was.  Sure, you may say that "I only put vertex shaders into this member so this IUnknown must be a vertex shader", but that fails to protect you against programmer error, which really misses the point of stronger typing.

Share this post


Link to post
Share on other sites
mhagain    13430

You don't have to cast it directly back to its original interface, you can use QueryInterface.

 

That would be safer, yes, but at the expense of having to Release the returned interface after every use.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this