Member Avatar for Thew

Hello,
I have some problems always when I'm trying to work with pointer-to-function. I need to create some that can be called from another class. But maybe I don't know how to make it. This is the code I've written:
Editor[.h]

//...
// UIRWin is the class that I need to call the MTP::Application::Start function from
// MTP::Application is in RenderEngine
UIRWin->mExtVoid = &MTP::Application::Start;
//...

RenderEngine[.h] contains class that have this function:

class Application
{
//...
virtual void Start(bool);
//...
}

UIRWin[.h]:

//...
typedef void (MTP::Application::*pVoid)(bool);
pVoid mExtVoid; // set in Editor
//...
if (mExtVoid)
    mExtVoid(true);

So everytime I try to compile this, I get this error:
error C2064: term does not evaluate to a function taking 1 arguments

Please, where I'm wrong? :icon_cry:

>Please, where I'm wrong?
You're mixing up pointers to functions and pointers to member functions. A pointer to a member function (which is what you want) must be called on an existing object:

MTP::Application obj;

(obj.*mExtVoid) ( true );
Member Avatar for Thew

So how can I create a pointer to function type if the function I want to call may be from different classes. For example:
class ClassOne
{
void functionOne(bool);
}
class ClassTwo
{
void functionTwo(bool);
}

typedef void(*pVoid)(bool);

and then set it like this:
pVoid mExternalVoid = ClassOneVar.functionOne;

or
pVoid mExternalVoid = ClassTwoVar.functionTwo;

>So how can I create a pointer to function type if the
>function I want to call may be from different classes.
You'll need to think of another way to accomplish what you want. The short answer is that pointers to members don't allow you to do this because a pointer to a member is intrinsically bound to a single class. It helps to understand that pointers can hold more information than just an address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.