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. Assigning pointers from one class to another and

Assigning pointers from one class to another and

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardwarehelptutorial
8 Posts 2 Posters 2 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.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I think I finally outdone myself. #1 In have class member function to assign pointer to structure from another class. No matter what I do I cannot make the compiler happy. Closes to "solution" was "need to use static" than when using static I got incomplete type, again. Originally this function was global and the pointer in question was also globally accessible ( function); The pointer ends up in globally accessible struct, so this exercise may be futile anyway. One solution may be to move the assigning function, but I am not sure of its use down the line, I am not there yet, I am about to give up and make the function global and be done with it. OK that one is fixed moved the pointer assinge,t function DONE #2 I access a class function via pointer however the function calls the calling call method in sort of circular fashion. Again because originally the app was classless , flat. Any suggestion how to solve this issue? I really like to keep the functions / methods in relations to hardware , each in its own class. Sorry for being such pest. Vaclav void HCD_SetEnumerationStartFunction(void (*ResetEnd)(void)) { hcd_ControlStructure.ResetEnd = ResetEnd; } hcd->HCD_SetEnumerationStartFunction(A_USBD::USBD_GetDeviceDescriptorBegin);

    L 1 Reply Last reply
    0
    • V Vaclav_

      I think I finally outdone myself. #1 In have class member function to assign pointer to structure from another class. No matter what I do I cannot make the compiler happy. Closes to "solution" was "need to use static" than when using static I got incomplete type, again. Originally this function was global and the pointer in question was also globally accessible ( function); The pointer ends up in globally accessible struct, so this exercise may be futile anyway. One solution may be to move the assigning function, but I am not sure of its use down the line, I am not there yet, I am about to give up and make the function global and be done with it. OK that one is fixed moved the pointer assinge,t function DONE #2 I access a class function via pointer however the function calls the calling call method in sort of circular fashion. Again because originally the app was classless , flat. Any suggestion how to solve this issue? I really like to keep the functions / methods in relations to hardware , each in its own class. Sorry for being such pest. Vaclav void HCD_SetEnumerationStartFunction(void (*ResetEnd)(void)) { hcd_ControlStructure.ResetEnd = ResetEnd; } hcd->HCD_SetEnumerationStartFunction(A_USBD::USBD_GetDeviceDescriptorBegin);

      L Offline
      L Offline
      leon de boer
      wrote on last edited by
      #2

      You keep doing this over and over .. think about it A_USBD::USBD_GetDeviceDescriptorBegin IS NOT AND NEVER WILL be a "void (*) void" function. It's a class member function and so it will be a "USBD::void(*)void" If you need to check stuff the search string "Member function pointer" will get you what you need. However I strongly advise you to stop and think for a minute before you start passing member function pointers around. If you need to do this a lot you might as well pass the whole class in as a pointer it is only 4 bytes and save the effort. Here lets cross connect both classes via class instance pointers

      // You will need the forward declares
      class USDB;
      class HCD;

      class USBD {
      public:
      void ConnectHCD (HCD* hcd); // Connect a HCD pointer into a USBD
      void SomeFunction (void); // Some test function

      HCD\* ConnectedHCD; // Pointer to connected buddy class it will be NULL after construction
      

      };

      class HCD {
      public:
      void ConnectUSBD (USBD* usbd); // Connect a USBD into a HCD

      USBD\* ConnectedUSBD; // Pointer to connected buddy class it will be NULL after construction
      

      };

      /* Simply hold given HCD pointer .. allows connectedHCD to be private if you want */
      void USBD::ConnectHCD (HCD* hcd) {
      ConnectedHCD = hcd;
      }

      /* Our test function */
      void USBD::SomeFunction (void){
      }

      /* Simply hold given USBD pointer ... allows connectedUSBD to be private if you want */
      void HCD::ConnectUSBD (USBD* usbd) {
      ConnectedUSBD = usbd;
      }

      // SO LETS USE IT IN YOUR CODE

      /* First we create our class objects */
      USBD usbd; // Create a usbd
      HCD hcd; // Create a hcd

      /* Then we connect them */
      usbd.ConnectHCD(&hcd); // Connect the created hcd as a pointer into usbd
      hcd.ConnectUSBD(&usbd); // Connect the created usbd as a pointer into hcd

      // Done you may now freely use the each class inside the other using the pointer
      // Dead simple and get around all the mess you are doing .. so lets do one
      hcd.ConnectedUSBD->SomeFunction(); // Just call it via the the pointer

      See each class can freely use it's connect buddy via the connected pointer. You just need to remember to connect them before you ever attempt to use them as the pointers will always start as null until set. Usually if you have to do this you have a basic design flaw but you can do it.

      In vino veritas

      V 1 Reply Last reply
      0
      • L leon de boer

        You keep doing this over and over .. think about it A_USBD::USBD_GetDeviceDescriptorBegin IS NOT AND NEVER WILL be a "void (*) void" function. It's a class member function and so it will be a "USBD::void(*)void" If you need to check stuff the search string "Member function pointer" will get you what you need. However I strongly advise you to stop and think for a minute before you start passing member function pointers around. If you need to do this a lot you might as well pass the whole class in as a pointer it is only 4 bytes and save the effort. Here lets cross connect both classes via class instance pointers

        // You will need the forward declares
        class USDB;
        class HCD;

        class USBD {
        public:
        void ConnectHCD (HCD* hcd); // Connect a HCD pointer into a USBD
        void SomeFunction (void); // Some test function

        HCD\* ConnectedHCD; // Pointer to connected buddy class it will be NULL after construction
        

        };

        class HCD {
        public:
        void ConnectUSBD (USBD* usbd); // Connect a USBD into a HCD

        USBD\* ConnectedUSBD; // Pointer to connected buddy class it will be NULL after construction
        

        };

        /* Simply hold given HCD pointer .. allows connectedHCD to be private if you want */
        void USBD::ConnectHCD (HCD* hcd) {
        ConnectedHCD = hcd;
        }

        /* Our test function */
        void USBD::SomeFunction (void){
        }

        /* Simply hold given USBD pointer ... allows connectedUSBD to be private if you want */
        void HCD::ConnectUSBD (USBD* usbd) {
        ConnectedUSBD = usbd;
        }

        // SO LETS USE IT IN YOUR CODE

        /* First we create our class objects */
        USBD usbd; // Create a usbd
        HCD hcd; // Create a hcd

        /* Then we connect them */
        usbd.ConnectHCD(&hcd); // Connect the created hcd as a pointer into usbd
        hcd.ConnectUSBD(&usbd); // Connect the created usbd as a pointer into hcd

        // Done you may now freely use the each class inside the other using the pointer
        // Dead simple and get around all the mess you are doing .. so lets do one
        hcd.ConnectedUSBD->SomeFunction(); // Just call it via the the pointer

        See each class can freely use it's connect buddy via the connected pointer. You just need to remember to connect them before you ever attempt to use them as the pointers will always start as null until set. Usually if you have to do this you have a basic design flaw but you can do it.

        In vino veritas

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        Thanks Leon, after good night sleep I figured it out and came to same conclusion - it it all about function pointer, again. I'll get it eventually. Cheers Vaclav

        L 1 Reply Last reply
        0
        • V Vaclav_

          Thanks Leon, after good night sleep I figured it out and came to same conclusion - it it all about function pointer, again. I'll get it eventually. Cheers Vaclav

          L Offline
          L Offline
          leon de boer
          wrote on last edited by
          #4

          No worries just remember classes always push a self pointer down to the call stack before any call to a function. That is something a static function pointer can not do and why it needs special syntax. If you just think of a class as a glorified C struct that can hold code as well as data and you will have an exact understanding of what is happening. The code block in two instances of the class run exactly the same code the only thing that changes is the code pulls back different self pointers which where pushed down to make the call. So the same code on different class instances changes data in two different memory blocks because "self" changed.

          In vino veritas

          V 1 Reply Last reply
          0
          • L leon de boer

            No worries just remember classes always push a self pointer down to the call stack before any call to a function. That is something a static function pointer can not do and why it needs special syntax. If you just think of a class as a glorified C struct that can hold code as well as data and you will have an exact understanding of what is happening. The code block in two instances of the class run exactly the same code the only thing that changes is the code pulls back different self pointers which where pushed down to make the call. So the same code on different class instances changes data in two different memory blocks because "self" changed.

            In vino veritas

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            Leon, I did check couple of "assigning pointers to class functions" articles. I'll need some time to digest it. Pretty strange syntax. I can see how "function to function" works, my problem is the way the original code was I have to eventually pass the pointer to struct and now it is in "standard" function pointer format ()(*)() . So I can either add a new member into the control struct ( it passes it to the ISR) or figure out how to convert the "member pointer" to it, if there is a need to do that. So I really need to study this first. Still makes me wonder why I picked such obscure "feature". Oh well. Cheers Vaclav

            L 1 Reply Last reply
            0
            • V Vaclav_

              Leon, I did check couple of "assigning pointers to class functions" articles. I'll need some time to digest it. Pretty strange syntax. I can see how "function to function" works, my problem is the way the original code was I have to eventually pass the pointer to struct and now it is in "standard" function pointer format ()(*)() . So I can either add a new member into the control struct ( it passes it to the ISR) or figure out how to convert the "member pointer" to it, if there is a need to do that. So I really need to study this first. Still makes me wonder why I picked such obscure "feature". Oh well. Cheers Vaclav

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #6

              So if that is the case you have a structural problem in how you organized code. However don't panic first get the code running and the easy way to do that is pass the whole class in to functions as a pointer .. it's only 4 bytes. You can pass the class pointer freely into another class lets say USBD needs HCD

              void USBD:: SomeFuncThatNeedsHCD (HCD* hcd){
              hcd->SomeHCDFunction();
              }

              or you can pass it to a static code block, lets say some static block needs HCD access

              static void SomeStaticCodeWhoAccessHCD (HCD* hcd){
              hcd->SomeHCDFunction();
              }

              As I said it's a symptom of a structural problem but a class is nothing more than a fancy struct it is legal to pass the class around as a pointer just like you did the original struct ... We just frown on it :-) Yes you can even pass the class pointer to an interrupt just make sure it is valid before the ISR uses. That usually means set it to NULL and the ISR checks for a non NULL value before it uses.

              In vino veritas

              V 1 Reply Last reply
              0
              • L leon de boer

                So if that is the case you have a structural problem in how you organized code. However don't panic first get the code running and the easy way to do that is pass the whole class in to functions as a pointer .. it's only 4 bytes. You can pass the class pointer freely into another class lets say USBD needs HCD

                void USBD:: SomeFuncThatNeedsHCD (HCD* hcd){
                hcd->SomeHCDFunction();
                }

                or you can pass it to a static code block, lets say some static block needs HCD access

                static void SomeStaticCodeWhoAccessHCD (HCD* hcd){
                hcd->SomeHCDFunction();
                }

                As I said it's a symptom of a structural problem but a class is nothing more than a fancy struct it is legal to pass the class around as a pointer just like you did the original struct ... We just frown on it :-) Yes you can even pass the class pointer to an interrupt just make sure it is valid before the ISR uses. That usually means set it to NULL and the ISR checks for a non NULL value before it uses.

                In vino veritas

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                Just an update. After all this messing around, I came to realize it all boils down to "plain" function pointer usage. I need to pay more attention to hardware instead of software. I essentially have two processes - one runs the video display and the other collects the video data using hardware interrupt. And there is the culprit - the hardware interrupts fires via single function pointer, and nothing but function pointer. No members function pointers, no class to class access via pointers - its all pretty much "down the hill" after the type of interrupt is determined. But I have learn few things and that is much appreciated. Thanks

                L 1 Reply Last reply
                0
                • V Vaclav_

                  Just an update. After all this messing around, I came to realize it all boils down to "plain" function pointer usage. I need to pay more attention to hardware instead of software. I essentially have two processes - one runs the video display and the other collects the video data using hardware interrupt. And there is the culprit - the hardware interrupts fires via single function pointer, and nothing but function pointer. No members function pointers, no class to class access via pointers - its all pretty much "down the hill" after the type of interrupt is determined. But I have learn few things and that is much appreciated. Thanks

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  Yes but that is easy to deal with fire the interrupt to a static function then kick it to the object. That solution is trivial.

                  PSOMECLASS* TheClassObject = NULL;

                  static void InterruptComesInHere (void){
                  if (TheClassObject) { // Make sure class/object has been set otherwise interrupt just returns
                  TheClassObject->CallSomeMethod(); // Send interrupt into class/object if set
                  }
                  }

                  All you have to do once you have your object initialized ready to receive is set the pointer to the created object using the & symbol. I would disable interrupts while you do the pointer set :-) All it really does is makes the interrupt a couple of cycles slower. The harder part by far is dealing with the interupts in windows which is nowhere near as easy as DOS. I assume this is a windows app and I wonder have you got the interrupt working because that isn't easy on windows 7,8 & 10 Windows uses hardware interrupts internally and does not let applications mess with them. You need to create a write a kernel-mode driver for that which is not trivial. To be honest in Windows at your level it would be easier to simply create a thread and poll the hardware for data and get rid of the interrupt all together. If you are trying to do this sort of thing commercially you are better off porting the IO onto a USB because they have raw block transfer mechanisms and the USB interface can be bought off the shelf. If you look up EasyCap for example which sells for about $10-$15 will drag normal S video into windows via the USB using exactly that trick.

                  In vino veritas

                  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