0

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

(something->*m_AddErrorMessage)(...)

8 Comments

If he's using m_AddErrorMessage unqualified then this is probably a CCalibration which isn't a CActionObjectBase. edit: you've fixed ->* / ->
@Charles You're right, I changed this-> to something-> instead.
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(&CActionObjectBase::AddResultStateErrorMessage);
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.