开发者

Function Pointer from base class

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:

开发者_如何学Pythonif(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


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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜