Error C2275 when creating a smart COM pointer in an "if" expression

Why i can't

if (IUnknownPtr p = anotherComPtr) {} //error C2275: 'IUnknownPtr' : illegal use of this type as an expression

while i can

if (int* a = anotherPointer) {}

IUnknownPtrdefined via t _COM_SMARTPTR_TYPEDEF(IUnknown, __uuidof(IUnknown))(like any other smart pointers that I use)

How can I create com smartptr in the instructions ifand check if it is really or not? Thank.

I am using VS 2008

ps This does not mean that it is a good way of coding, not about error C2275.

+5
source share
3 answers

I cannot reproduce the compiler error in vs2008 in the small program below. There is probably something else in your included files, preprocessor definitions, or compiler options that give you different behavior.

IUnknownPtr if?

, ?

OK?

if (NULL == (IUnknownPtr ptr = someOtherPtr)) {
}

IUnknownPtr foo;
bool b(foo);

, IUnknownPtr, IUnknownPtr bool.

operator = IUnknownPtr & (, ). _com_ptr_t bool(). _COM_SMARTPTR_TYPEDEF _com_ptr_t - ? , (/++// )

#include <comdef.h>

int main(int argc, char* argv[])
{
    IUnknownPtr foo;
    IUnknown* foo2 = NULL;

    if (IUnknownPtr foo3 = foo) {
        // do something
    }

    if (IUnknownPtr foo4 = foo2) {
        // do something
    }

    return 0;
}
+1

, MSDEV ++, if ( for?).

, .

, - MSDN? , , ( ) , .

?

0

Since this compiles for me just fine on VS 2010, I'm going to assume you are probably not #include <comdef.h>and / or #include <comdefsp.h>.

0
source

All Articles