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. a problem with function pointer

a problem with function pointer

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 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.
  • A Offline
    A Offline
    Alex Cutovoi
    wrote on last edited by
    #1

    Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable: unsigned int (__stdcall * ObjectEvent)(void*); This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See: typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData); This is my method signature: unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) {    MessageBox(NULL, "working", "Info", MB_OK);    return 1; } This is my code when I put the method of my class into the function poiner variable: void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) {    ...    ObjectEvent = ObjectCallBack;    m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); } HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this: error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)' So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.

    C CPalliniC D 3 Replies Last reply
    0
    • A Alex Cutovoi

      Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable: unsigned int (__stdcall * ObjectEvent)(void*); This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See: typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData); This is my method signature: unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) {    MessageBox(NULL, "working", "Info", MB_OK);    return 1; } This is my code when I put the method of my class into the function poiner variable: void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) {    ...    ObjectEvent = ObjectCallBack;    m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); } HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this: error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)' So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The problem comes from the fact that global functions and class functions don't have the same signature: for the class functions, the this (address of the instance of the class which call the function) is passed implicitely with the paramters. If you want to solve the problem, specify your function as statis but then the problem will be that you won't be able to access non-static members (the function doesn't belong to any instance). Again, you can solve this problem by using a global function as callback and pass the address of your instance (this pointer) to the hdScheduleAsynchronous function. Normally, every call back lets you do this. Then, inside your global callback function, cast the void pointer to a Object3DS* pointer and call a function from within your class. An even nicer way to do it is not to use a global function but a static function from your class.


      Cédric Moonen Software developer
      Charting control [v1.1]

      A 1 Reply Last reply
      0
      • A Alex Cutovoi

        Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable: unsigned int (__stdcall * ObjectEvent)(void*); This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See: typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData); This is my method signature: unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) {    MessageBox(NULL, "working", "Info", MB_OK);    return 1; } This is my code when I put the method of my class into the function poiner variable: void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) {    ...    ObjectEvent = ObjectCallBack;    m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); } HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this: error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)' So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        You can do that only with a static method of the class. But if your method has to access instance members then you must find a way to pass them (perhaps the whole instance of the class) to it.

        In testa che avete, signor di Ceprano?

        A 1 Reply Last reply
        0
        • A Alex Cutovoi

          Hi fellows I'm developing an app that needs to use function pointer. I'm assigning one of my methods in a function pointer variable. this is my fuction pointer variable: unsigned int (__stdcall * ObjectEvent)(void*); This variable needs to be like this because in the header file that I use there are a typedef with the same signature of my variable and some functions of this header that uses this typedef. See: typedef unsigned int (__stdcall *HDSchedulerCallback)(void *pUserData); This is my method signature: unsigned int __stadcall Object3DS::ObjectCallBack(void * vData) {    MessageBox(NULL, "working", "Info", MB_OK);    return 1; } This is my code when I put the method of my class into the function poiner variable: void Object3DS::LoadObject3DS(const char * cObjectName, bool bVisible, float fX, float fY, float fZ) {    ...    ObjectEvent = ObjectCallBack;    m_ObjectHandle = hdScheduleAsynchronous(ObjectEvent, 0, HD_DEFAULT_SCHEDULER_PRIORITY); } HD_DEFAULT_SCHEDULER_PRIORITY is just a thread priority variable. The error that I receive is this: error C2440: '=' : cannot convert from 'unsigned int (__stdcall Object3DS::*)(void *)' to 'unsigned int (__stdcall *)(void *)' So fellows what I have to do to solve this?? I've tried somethings but nothing solved. I haven't any idea.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Alex Cutovoi wrote:

          So fellows what I have to do to solve this??

          See here.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          A 1 Reply Last reply
          0
          • D David Crow

            Alex Cutovoi wrote:

            So fellows what I have to do to solve this??

            See here.


            "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

            "Judge not by the eye but by the heart." - Native American Proverb

            A Offline
            A Offline
            Alex Cutovoi
            wrote on last edited by
            #5

            Tks for you all. I hope that works now.

            1 Reply Last reply
            0
            • CPalliniC CPallini

              You can do that only with a static method of the class. But if your method has to access instance members then you must find a way to pass them (perhaps the whole instance of the class) to it.

              A Offline
              A Offline
              Alex Cutovoi
              wrote on last edited by
              #6

              Tks for you all. I hope that works now.

              1 Reply Last reply
              0
              • C Cedric Moonen

                The problem comes from the fact that global functions and class functions don't have the same signature: for the class functions, the this (address of the instance of the class which call the function) is passed implicitely with the paramters. If you want to solve the problem, specify your function as statis but then the problem will be that you won't be able to access non-static members (the function doesn't belong to any instance). Again, you can solve this problem by using a global function as callback and pass the address of your instance (this pointer) to the hdScheduleAsynchronous function. Normally, every call back lets you do this. Then, inside your global callback function, cast the void pointer to a Object3DS* pointer and call a function from within your class. An even nicer way to do it is not to use a global function but a static function from your class.


                Cédric Moonen Software developer
                Charting control [v1.1]

                A Offline
                A Offline
                Alex Cutovoi
                wrote on last edited by
                #7

                Tks for you all. I hope that works now.

                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