CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: C++ programming

  1. #1
    Join Date
    Apr 1999
    Posts
    3

    C++ programming

    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





  2. #2
    Join Date
    Apr 1999
    Location
    Monterrey, Mexico
    Posts
    21

    Re: C++ programming

    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...


  3. #3
    Join Date
    Apr 1999
    Posts
    90

    Re: C++ programming

    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?


  4. #4
    Guest

    Re: C++ programming

    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.



  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ programming

    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



  6. #6
    Join Date
    Apr 1999
    Location
    Monterrey, Mexico
    Posts
    21

    Re: C++ programming

    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


  7. #7
    Join Date
    Apr 1999
    Posts
    3

    Re: C++ programming

    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.


  8. #8
    Guest

    Re: C++ programming

    Thank you for your reply. It was very helpful.

    Anna


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured