Click to See Complete Forum and Search --> : C++ programming
Anna
April 13th, 1999, 01:25 PM
Hi
I am trying to figure out a way to assign a derived class's member function to a base class member function pointer.
example:
class base
{
public:
double func(int i);
};
class derived: public base
{
public:
double myfunc(int j);
};
double (base::*bptr)(int);
// assign derived class's member function to base
// class's member function pointer
bptr = &derived::myfunc; // but this is illegal to do
How can I resolve this? Any pointers would be greatly appreciated. Thank you in advance for your help.
Anna
eperales
April 13th, 1999, 02:40 PM
Maybe you need to explain us what kind of problem are you solving that requires to assign a derived class's member function to a base class member function pointer. Maybe rethinking your problem with virtual functions...
Michael Decker
April 13th, 1999, 02:48 PM
Since a class is an encapsulation of data as well as functions,why would you need to do what you are trying to do? If you could do what your asking, then you'd lose the notion of what ever data is suppose to be with that class.
Why do you think you need to do this?
April 13th, 1999, 02:55 PM
Hi
thanks for your reply. Let me first explain what I am trying to do here. I am porting some software written in Borland
C++ to visual C++(for reasons only known to some here). There is a keyword '__closure' in borland C++ that lets
assigning a derived class's member function to a base class member function pointer. This is extensively used in our code
in context to threads and notification events. I am trying to do the same in visual c++. hope this explains. thanks again for any help.
Paul McKenzie
April 13th, 1999, 04:47 PM
That's the price you have to pay when you use compiler specific extensions. I couldn't find "__closure" anywhere in my Borland 5.02 online help. However, with what you described of it, my opinion is that it was created so that someone who has only programmed 'C' and doesn't want to learn real OO techniques can get an app "out the door" as quickly as possible.
One question:
Why wasn't the use of virtual functions satisfactory in the original design? Polymorphism (aka virtual functions) is used in threaded programs all over the place. How would a language that relies so much on synchrousity and threading such as Java work if it wasn't safe to use object oriented techniques?
Regards,
Paul McKenzie
eperales
April 13th, 1999, 05:09 PM
I remember vaguely this C++ Builder feature. If I remember correcty you can "fix" some code using MFC message maps, but I think you may need to rewrite the code using virtual functions and/or abstract base classes. But it will be very time consuming :(
Tim G
April 13th, 1999, 11:39 PM
Anna, I don't know why everyone keeps questioning your motives instead of giving you an answer. Anyways, if you are using MSVC, you can use the reinterpret_cast<> keyword:
typedef double (base::*BPTR)(int);
BPTR bptr;
// assign derived class's member function to base
// class's member function pointer
bptr = reinterpret_cast< BPTR >( &derived::myfunc );
This will compile, I don't know if it works the way you want though. ( I tried compiling with MSVC 6.0 ). Good luck.
April 14th, 1999, 03:07 PM
Thank you for your reply. It was very helpful.
Anna
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.