Re: type safe issue
George <George@discussions.microsoft.com> wrote:
I think you mean the solution I posted can solve type safe issue to
some extent, but not solving all issues, for example, this is the
issue you pointed,
Well, if you are hell-bent on breaking this solution, you can of
course write
IComPtr<IY, &IID_IZ> p;
p.CreateInstance(...);
and using CComPtr smart pointer can solve the issue mentioned above,
template <typename I, IID* iid = &__uuidof(I)>
class CComPtr {...};
Actually, I double-checked and discovered CComPtr doesn't take the
second parameter, but relies exclusively on __uuidof to obtain the IID.
You can still break it, by hand-crafting a __declspec(uuid) declaration
that associates an interface with a wrong IID.
CComQIPtr does take IID as an optional second parameter, and you can
break it like this:
CComQIPtr<IY, &IID_IZ> p;
p.CoCreateInstance(...);
The point is not to construct an absolutely fool-proof solution: it
doesn't exist, you can always hurt yourself *on purpose* if you really
want to (though it's unclear why you would want to). The point is only
to construct a solution that is difficult to misuse *accidentally*. Look
up the difference between "on purpose" and "accidentally" in your
favorite English dictionary.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925