I work with TaskScheduler COM, this is my code:
typedef HRESULT(*FuncOfBoll)(_Out_ VARIANT_BOOL* b);
static bool GetBool(FuncOfBoll func)
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr = func(&b);
if (FAILED(hr)) return FALSE;
return b == VARIANT_TRUE;
}
void test(ITaskSettings* settings)
{
bool b = GetBool(settings->get_StopIfGoingOnBatteries); // <= The error here
// ...
}
and I get the following error:
Error C3867 'ITaskSettings::get_StopIfGoingOnBatteries': non-standard
syntax; use '&' to create a pointer to member
What is my mistake and how to correct it?
The correct definition for a pointer to member function is:
typedef HRESULT(ITaskSettings::*FuncOfBoll)(_Out_ VARIANT_BOOL* b);
Then, you should pass the pointer to the object instance to function GetBool
:
static bool GetBool(ITaskSettings* setting, FuncOfBoll func)
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr = (setting->*func)(&b);
...
}
Or, with template:
template<class C>
static bool GetBool(C* p, HRESULT(C::*func)(_Out_ VARIANT_BOOL*))
{
VARIANT_BOOL b = VARIANT_FALSE;
HRESULT hr = (p->*func)(&b);
...
}
Invocation:
void test(ITaskSettings* settings)
{
currentSetttings = settings;
bool b = GetBool(settings, &ITaskSettings::mb_function);
}
I am guessing that get_StopIfGoingOnBatteries
is a member function ofITaskSettings
. Such a function cannot be used when the expected argument type is FuncOfBoll
. You'll need to create a wrapper function and use it.
ITaskSettings* currentSetttings = NULL;
HRESULT GetBoolWrapper(_Out_ VARIANT_BOOL* b)
{
return currentSetttings->get_StopIfGoingOnBatteries(b);
}
void test(ITaskSettings* settings)
{
currentSetttings = settings;
bool b = GetBool(GetBoolWrapper);
}
This is not really an answer, the question as written doesn't admit an answer, but this is too long & detailed for comment.
You don't show a full examples, so there has to be guesswork.
Apparently, judging by the error message, settings
is a pointer to class type object, where that class has a member function get_StopIfGoingOnBatteries
.
And apparently, judging by the use of ->
operator, it's a non-static
member function. You could still use ->
if it were static
, but that would be unnatural. So let's say it's a non-static
member function.
Then you can't easily form a raw pointer to function that calls that member function, because you need a this
-pointer for the call. It could just use a dummy object, if creation of such object is cheap, or it could use a global instance or pointer to instance. But better change something in your design, and/or explain more clearly what you want.