member functions pointers and subclasses

L

lallous

Hello

#include <iostream>

#define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))

class Fred;
typedef int (Fred::*FredMemFn)(char x, float y);
class Fred
{
private:
FredMemFn m_fn;

public:
int f(char x, float y);
int g(char x, float y);
int h(char x, float y) { std::cout << "Fred's h()" << std::endl; return
0;}
int i(char x, float y);
void SetFN(FredMemFn f) { m_fn = f; }
void carry()
{
CALL_MEMBER_FN(*this, m_fn)('x', 1);
}
};


class CMyFred : public Fred
{
public:
int x(char a, float b)
{
std::cout << "MyFred's x()" << std::endl;
}
void test();
};

void CMyFred::test()
{
// this works
SetFN(Fred::h);

// this doesn't <-- how to make it work?
SetFN(x);
}

int main()
{
CMyFred f;

f.test();

f.carry(); // calls Fred's m_fn()

return 0;
}

How can I make this code work if I want class Fred/carry() to call handlers
set in CMyFred ?

If typedef int (Fred::*FredMemFn)(char x, float y) is a pointer to a Fred
member function, won't a member function in CMyFred (subclass of Fred) with
same signature be accepted?

Hope my question was clear

Regards,
Elias
 
D

David Harmon

On Fri, 23 Apr 2004 15:02:48 +0300 in comp.lang.c++, "lallous"
typedef int (Fred::*FredMemFn)(char x, float y); ....
// this doesn't <-- how to make it work?
SetFN(x);

Hmmm. Digital Mars C++ http://www.digitalmars.com compiles it as
written.

MSVC 6.0 requires the cast
SetFN(FredMemFn(x));

Who's right?
 
L

lallous

David Harmon said:
On Fri, 23 Apr 2004 15:02:48 +0300 in comp.lang.c++, "lallous"


Hmmm. Digital Mars C++ http://www.digitalmars.com compiles it as
written.

MSVC 6.0 requires the cast
SetFN(FredMemFn(x));

Who's right?


Hello David,

Let us not bother who is right or wrong.

Perhaps you can help me solve my problem, if I describe my problem
again:

I have a base class which has a certain message handler that will call
a set of registered callbacks.
The callbacks have a class member function pointer prototype, each
callback is inserted say into the map<int id, callback>

Now another class will be derived from the base class and will insert
some callbacks.

But the base class, on a given time, it will search the map for a
given ID and trigger the callback which resides in the derived class.

A simple illustration:

class CBase
{
private:
callbackmap_t cbmap;
public:
void handler()
{
// wait for an input, convert input to ID
cbmap[id](); // trigger the callback
}
};

class CDerived: public CBase
{
public:
void handler1() { }
void handler2() { }
void init()
{
addcb(handler1, 1); // call base's addcb() and register a cb with
Id=1
addcb(handler2, 2); // ...
}
};

Now in main:
CDerived c;
c.init();
c.go(); // call base's go() which will start message loop
// and route some events to appropriate registered callbacks

Regards,
Elias
 
D

DaKoadMunky

I cannot comment on what the standard says about this, but it seems to me that
placing a derived class function address into a base class function pointer
would be a bad thing.

Please consider the following...

Code:
struct Base
{
};

struct Derived : Base
{
void SomeFunc()
{
}
};

int main()
{
typedef void (Base::*BMF)();

BMF bmf = (BMF)Derived::SomeFunc;

Base instanceOfBase;

(instanceOfBase.*bmf)();

return 0;
}

Can you see in this example that I am invoking a derived class member function
whose "this" pointer does not point to a derived class object?

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top