tags:

views:

54

answers:

2

Hi there,

i need a Function Pointer from a base class. Here is the code:

class CActionObjectBase
{
  ...
  void AddResultStateErrorMessage( const char* pcMessage , ULONG iResultStateCode);
  ...
}

CActionObjectCalibration( ): CActionObjectBase()
{
 ...
 m_Calibration = new CCalibration(&CActionObjectBase::AddResultStateErrorMessage); 
}

class CCalibration
{
 ...
 CCalibration(void (CActionObjectBase::* AddErrorMessage)(const char*, ULONG ));
 ...
 void (CActionObjectBase::* m_AddErrorMessage)(const char*, ULONG );
}

Inside CCalibration in a Function occurs the Error. I try to call the Function Pointer like this:

if(m_AddErrorMessage)
{
 ...
 m_AddErrorMessage("bla bla", RSC_FILE_ERROR);
}

The Problem is, that I cannot compile. The Error Message says something like: error C2064: Expression is no Function, that takes two Arguments.

What is wrong?

regards camelord

A: 

You need to invoke m_AddErrorMessage on an object, something like:

(something->*m_AddErrorMessage)(...)
Andreas Brinck
If he's using `m_AddErrorMessage` unqualified then this is probably a `CCalibration` which isn't a `CActionObjectBase`. edit: you've fixed `->*` / `->`
Charles Bailey
@Charles You're right, I changed `this->` to `something->` instead.
Andreas Brinck
Do i really need to pass an Object of CActionObjectBase to CCalibration? I tried to avoid this by using a Function Pointer as Parameter. Then i can call the CCalibration Function with an Object, derived from CActionObjectBase, calling the Function like i tried to show above:m_Calibration = new CCalibration(
camelord
It's not a function pointer, it's a pointer to a member and you can't call a member function (whether via a pointer to member or directly) without having an object to call it on. So yes, you do need an object to call the function on.
Charles Bailey
@Camelord As both me and Charles have stated you need an object to call the member function pointer on, the pointer to `AddResultStateErrorMessage` is the same for all instances of `CActionObjectBase`.
Andreas Brinck
Well, if i understood it correct, i can't call the Function via Function Pointer, even if i pass the Pointer of the Function, without the Object itself. In this case i can kick the Function Pointer and just pass the Object to CCalibration..
camelord
@camelord: A pointer-to-member is not a function pointer. They are very different types and this may be the source of your misunderstanding. If you don't need to abstract which member function is being called then a pointer to member is unnecessary complexity. Which ever way you go, if you want to call a member function you need a pointer or reference to the object on which you want to call it.
Charles Bailey
Ok, i guess i got it now.. Thx for youre help
camelord
+2  A: 

You need to provide an object on which you call the member function:

CActionObjectBase* pActionObjectBase /* = get pointer from somewhere */ ;

(pActionObjectBase->*m_AddErrorMessage)("bla bla", RSC_FILE_ERROR);

Unlike normal object and function pointers, pointers to members can only be deferenced using an object of the appropriate type via the .* (for objects and references) or ->* (for pointers to objects) operators.

Charles Bailey