Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. pointer to virtual function

pointer to virtual function

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RalfPeter
    wrote on last edited by
    #1

    Hi all, this is the error I'm getting: "error C2276: '&' : illegal operation on bound member function expression", Apparently, this is due to assigning a pointer to a virtual function, yet I do not have any function declared virtual. Does any one know a way around this problem? Is there a way to force a function not to be virtual? Here's a snippet of my code: In .h … class MyClass {… int VBGCalc(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J); int VBGPartials(const gsl_vector *x, void *params, gsl_matrix *J); int VBGFunct(const gsl_vector *x, void *params, gsl_vector *f); int VBGF(void); //problem in this function … }; in .cpp int MyClass::VBGF(void) … f.f = &VBGFunct; //offending code f.df = &VBGPartials; //ditto f.fdf = &VBGCalc; //ditto … return 1; } int (* f) (const gsl_vector * x, void * params, gsl_vector * f); gsl_vector and gsl_matrix are struct variables. Thanks, Ralf. ralf.riedel@usm.edu

    M P 2 Replies Last reply
    0
    • R RalfPeter

      Hi all, this is the error I'm getting: "error C2276: '&' : illegal operation on bound member function expression", Apparently, this is due to assigning a pointer to a virtual function, yet I do not have any function declared virtual. Does any one know a way around this problem? Is there a way to force a function not to be virtual? Here's a snippet of my code: In .h … class MyClass {… int VBGCalc(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J); int VBGPartials(const gsl_vector *x, void *params, gsl_matrix *J); int VBGFunct(const gsl_vector *x, void *params, gsl_vector *f); int VBGF(void); //problem in this function … }; in .cpp int MyClass::VBGF(void) … f.f = &VBGFunct; //offending code f.df = &VBGPartials; //ditto f.fdf = &VBGCalc; //ditto … return 1; } int (* f) (const gsl_vector * x, void * params, gsl_vector * f); gsl_vector and gsl_matrix are struct variables. Thanks, Ralf. ralf.riedel@usm.edu

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      You can't assign a member function pointer to an ordinary function pointer. The type of VBGFunct is int (MyClass::*) (const gsl_vector *, void *, gsl_vector *). You can't take the address of a member function pointer. You would have to either declare f as int (MyClass::*f)( /*etc*/ ) or declare your functions as static. If you do the first, you must call them using the ->* or .* notation, e.g.:

      (this->*f)(pX, pParams, pF)

      If you do the second, you won't be able to access any non-static members of MyClass. If you're trying to provide polymorphism, it's easier to either do run-time polymorphism via inheritance, or compile-time polymorphism using templates. Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • R RalfPeter

        Hi all, this is the error I'm getting: "error C2276: '&' : illegal operation on bound member function expression", Apparently, this is due to assigning a pointer to a virtual function, yet I do not have any function declared virtual. Does any one know a way around this problem? Is there a way to force a function not to be virtual? Here's a snippet of my code: In .h … class MyClass {… int VBGCalc(const gsl_vector *x, void *params, gsl_vector *f, gsl_matrix *J); int VBGPartials(const gsl_vector *x, void *params, gsl_matrix *J); int VBGFunct(const gsl_vector *x, void *params, gsl_vector *f); int VBGF(void); //problem in this function … }; in .cpp int MyClass::VBGF(void) … f.f = &VBGFunct; //offending code f.df = &VBGPartials; //ditto f.fdf = &VBGCalc; //ditto … return 1; } int (* f) (const gsl_vector * x, void * params, gsl_vector * f); gsl_vector and gsl_matrix are struct variables. Thanks, Ralf. ralf.riedel@usm.edu

        P Offline
        P Offline
        Prakash Nadar
        wrote on last edited by
        #3

        member functions are of __THISCALL so i guess assigning a pointer to the method could of some problem and also using it.. try making the methods static i guess then it will work... But then i dont know wheather making them static is in ur software design.


        MSN Messenger. prakashnadar@msn.com "If history isn't good, just burn it." - Sidhuism.

        R 1 Reply Last reply
        0
        • P Prakash Nadar

          member functions are of __THISCALL so i guess assigning a pointer to the method could of some problem and also using it.. try making the methods static i guess then it will work... But then i dont know wheather making them static is in ur software design.


          MSN Messenger. prakashnadar@msn.com "If history isn't good, just burn it." - Sidhuism.

          R Offline
          R Offline
          RalfPeter
          wrote on last edited by
          #4

          Made it static. Much easier. Thank you all, Ralf. ralf.riedel@usm.edu

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups