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. Function pointer point to a object member function in C++?

Function pointer point to a object member function in C++?

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++designregex
3 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.
  • L Offline
    L Offline
    lvantin
    wrote on last edited by
    #1

    Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,

    S A 2 Replies Last reply
    0
    • L lvantin

      Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      Here is the long version: Member Function Pointers and the Fastest Possible C++ Delegates But this site may be more helpful to start with: http://www.parashift.com/c++-faq-lite/pointers-to-members.html[^]

      1 Reply Last reply
      0
      • L lvantin

        Hi everyone, Have a nice day, please help me following concern I know that, Function pointer in C++ can point to an address of global function, or static public function of a class For example: typedef float (MyFunction)(float a, float b); //Global function float add(float a, float b) { return a+b; } Class Math { public static float add(float a, float b) { return a+b; } } void main(char*args[]) { MyFunction*fp; //Point to global function fp = add; printf("%f",fp(1,2)) //Point to public static function fp = Math::add; printf("%f",fp(5,10)); } But it can not point to address of a function of a specified object. For example, function add in calss Math is not static Class Math { public float add(float a, float b) { return a+b; } } The following is impossible (compile error) void main(char*args[]) { MyFunction*fp; //Point to public function of specified object instance Math*m = new Math(); fp = m->add; printf("%f",fp(5,10)); } But it is the thing i really want. Because i want to know function add is executed in context of what object (in this case we can know it is object m). Above example is something like the concept Delegate in .NET. Is there any design pattern to make such a delegate for C++? Thanks, Tin,

        A Offline
        A Offline
        App_
        wrote on last edited by
        #3

        Class Math <--- error
        {
        public float add(float a, float b)
        {
        return a+b;
        }
        }<--- error

        Firstly , lowercase. Class Math should be class Math secondly , semicolon

        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