473,795 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

member functions pointers and subclasses

Hello

#include <iostream>

#define CALL_MEMBER_FN( object,ptrToMem ber) ((object).*(ptr ToMember))

class Fred;
typedef int (Fred::*FredMem Fn)(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::*FredMem Fn)(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
Jul 22 '05 #1
3 1613
On Fri, 23 Apr 2004 15:02:48 +0300 in comp.lang.c++, "lallous"
<la*****@lgwm.o rg> wrote,
typedef int (Fred::*FredMem Fn)(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?

Jul 22 '05 #2
David Harmon <so****@netcom. com> wrote in message news:<40******* *********@news. west.earthlink. net>...
On Fri, 23 Apr 2004 15:02:48 +0300 in comp.lang.c++, "lallous"
<la*****@lgwm.o rg> wrote,
typedef int (Fred::*FredMem Fn)(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?

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
Jul 22 '05 #3
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...

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct Base
  3. {
  4. };
  5.  
  6. struct Derived : Base
  7. {
  8. void SomeFunc()
  9. {
  10. }
  11. };
  12.  
  13. int main()
  14. {
  15. typedef void (Base::*BMF)();
  16.  
  17. BMF bmf = (BMF)Derived::SomeFunc;
  18.  
  19. Base instanceOfBase;
  20.  
  21. (instanceOfBase.*bmf)();
  22.  
  23. return 0;
  24. }
  25.  
  26.  
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


Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1681
by: Thomas Matthews | last post by:
Hi, I have a hierarchy of classes and would like to create an array of pointers to member functions that could also contain pointers to the parent member functions as well. What is the syntax for this? #include <string> using std::string;
12
2093
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager, so he will send us all on a conversion course. One of many reasons is that our code is littered with examples of: SUBROUTINE PRINT_ITEM(ITEM, ITEM_TYPE) IF (ITEM_TYPE .EQ. SQUARE) THEN CALL PRINT_SQUARE(ITEM)
2
3640
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
22
2963
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give me some hints on what might be wrong? // BEGIN OF SAMPLE CODE class Strategy
3
1711
by: Gregory Bond | last post by:
I'm building a class hierarchy that needs to keep as a class variable a reference to a (non-member) function, so that different subclasses can use different generator functions. But it seems Python is treating the function as a member function because the reference to it is in class scope.... Here's a simple case of what I'm trying (in the real code, fn is a function that returns a database connection relevant to that subclass): >...
11
4434
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
1
7592
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
14
4212
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
3
2827
by: Ramesh | last post by:
Hi, I am trying to create an array of pointers to member functions inside my class. When I created a global array of type pfn & initialized with member functions and copy it back to the member function pointer array (Handlers) - compiler doesnt let me do that. I got an error as below: "invalid use of non-static member function bool test::func1(long int)"
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.