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. How can I create a generic pointer?

How can I create a generic pointer?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 5 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.
  • X Offline
    X Offline
    xonobo
    wrote on last edited by
    #1

    Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo

    E J 2 Replies Last reply
    0
    • X xonobo

      Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo

      E Offline
      E Offline
      Emmanouil
      wrote on last edited by
      #2

      you just write: void * myPntr;

      X 1 Reply Last reply
      0
      • X xonobo

        Hi list, I want to ask whether is it possible to have a pointer whose type can change according to the input. I want to make it clear waht I need by a little code below which is not possible to compile. I think there must be a very well known trick for this. Thanks in advance for your help. switch( datatype ) { case DT_INT8: char *myPntr = (char *)dataPntr case DT_UINT8: unsigned char *myPntr = (unsigned char*)dataPntr; case DT_INT16: short *myPntr = (short *)dataPntr; } dataPntr is a (void *). how can I create such myPntr? xonobo

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        xonobo wrote:

        want to ask whether is it possible to have a pointer whose type can change according to the input.

        A void* can point to anything, but in turn it looses the information to what it is pointing. You need to bundle this information with it. In VB this is done using the VARIANT struct. That is a struct holding a type marker and a piece of memory. Via a union, all datatypes are 'layered" above each other, using the same memory.


        Failure is not an option - it's built right in.

        X 1 Reply Last reply
        0
        • E Emmanouil

          you just write: void * myPntr;

          X Offline
          X Offline
          xonobo
          wrote on last edited by
          #4

          just read the reply of jhwurmbach below. That's my problem. void * do not help in that. but thanks...

          1 Reply Last reply
          0
          • J jhwurmbach

            xonobo wrote:

            want to ask whether is it possible to have a pointer whose type can change according to the input.

            A void* can point to anything, but in turn it looses the information to what it is pointing. You need to bundle this information with it. In VB this is done using the VARIANT struct. That is a struct holding a type marker and a piece of memory. Via a union, all datatypes are 'layered" above each other, using the same memory.


            Failure is not an option - it's built right in.

            X Offline
            X Offline
            xonobo
            wrote on last edited by
            #5

            Thanks a lot for your reply and ask: anyone knows a solution for C/C++?

            J S 2 Replies Last reply
            0
            • X xonobo

              Thanks a lot for your reply and ask: anyone knows a solution for C/C++?

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              You don't tell enough to get a solution to your problem. What are you trying to accomplish and why can't this be solved in another way? Does the (Microsoft specific) _variant_t help you?


              Failure is not an option - it's built right in.

              X 1 Reply Last reply
              0
              • J jhwurmbach

                You don't tell enough to get a solution to your problem. What are you trying to accomplish and why can't this be solved in another way? Does the (Microsoft specific) _variant_t help you?


                Failure is not an option - it's built right in.

                X Offline
                X Offline
                xonobo
                wrote on last edited by
                #7

                Thamks for your help. I thought VARIANT is only available in VB that's why I asked more. Now I will check _variant_t. Thanks again.

                D 1 Reply Last reply
                0
                • X xonobo

                  Thamks for your help. I thought VARIANT is only available in VB that's why I asked more. Now I will check _variant_t. Thanks again.

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

                  xonobo wrote:

                  I thought VARIANT is only available in VB...

                  It's definitely not limited to VB.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • X xonobo

                    Thanks a lot for your reply and ask: anyone knows a solution for C/C++?

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    Try http://www.boost.org/doc/html/variant.html[^] and http://www.boost.org/doc/html/any.html[^].

                    Steve

                    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