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

Started by
3 comments, last by 21st Century Moose 11 years ago

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?

Advertisement

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.

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement