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. runtime type casting

runtime type casting

Scheduled Pinned Locked Moved C / C++ / MFC
c++
5 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.
  • W Offline
    W Offline
    wallbrick
    wrote on last edited by
    #1

    I'm new to c++ and this forum, so please... I want to convert one type(int/float/double, can be 8/16/32/64bits), which is unknow when compiling, to another type, which is also unknow at compile time, in some function. One way is to use a vitual function and override that function with different arugment types. It's a heavy function with a lot of coding, so I want to search some other options. I'm wondering if there is a solution so that I only need to write one conversion function which can take care of whatever input/output types. Here are some pseudo codes which I'm not sure if they can be somehow realized. Hope it wouldn't confuse you. define type T*; Convert( T* in, T* out) { for_each_datum_in { *in=(type of out)*out; } } //call the function here Convert( TypeOfIn * in, TypeOfOut * out);

    now here am i

    M L N 4 Replies Last reply
    0
    • W wallbrick

      I'm new to c++ and this forum, so please... I want to convert one type(int/float/double, can be 8/16/32/64bits), which is unknow when compiling, to another type, which is also unknow at compile time, in some function. One way is to use a vitual function and override that function with different arugment types. It's a heavy function with a lot of coding, so I want to search some other options. I'm wondering if there is a solution so that I only need to write one conversion function which can take care of whatever input/output types. Here are some pseudo codes which I'm not sure if they can be somehow realized. Hope it wouldn't confuse you. define type T*; Convert( T* in, T* out) { for_each_datum_in { *in=(type of out)*out; } } //call the function here Convert( TypeOfIn * in, TypeOfOut * out);

      now here am i

      M Offline
      M Offline
      Matthew Faithfull
      wrote on last edited by
      #2

      Strictly speaking you can't do what you've asked about in ordinary C++. Only using .NET reflection, but the chances are that the set of types is not really 'unknown' at compile time it's just that you don't want to have to write much or any new code when you change that set? If that's true you have a chance. Consider using templates e.g. template< typename Ti, typename To > Convert( Ti* pIn, To* pOut ) { //Conversion code would go here but it's going to have to make //some assumptions about the types in order to work //which means it won't work with just anything :( } In the end you'll probably have to provide lots of type specific template overrides like Convert( double* pIn, char* pOut ) { //a call to printf may occur somewhere in here :omg: } which is not much better than where you started out. If you've hit this problem and most of us have :doh: then you probably need to go back and look at your design with a 'what is this really try to achieve' hat on :)

      Nothing is exactly what it seems but everything with seems can be unpicked.

      1 Reply Last reply
      0
      • W wallbrick

        I'm new to c++ and this forum, so please... I want to convert one type(int/float/double, can be 8/16/32/64bits), which is unknow when compiling, to another type, which is also unknow at compile time, in some function. One way is to use a vitual function and override that function with different arugment types. It's a heavy function with a lot of coding, so I want to search some other options. I'm wondering if there is a solution so that I only need to write one conversion function which can take care of whatever input/output types. Here are some pseudo codes which I'm not sure if they can be somehow realized. Hope it wouldn't confuse you. define type T*; Convert( T* in, T* out) { for_each_datum_in { *in=(type of out)*out; } } //call the function here Convert( TypeOfIn * in, TypeOfOut * out);

        now here am i

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        Is there some reason a template function won't work?

        1 Reply Last reply
        0
        • W wallbrick

          I'm new to c++ and this forum, so please... I want to convert one type(int/float/double, can be 8/16/32/64bits), which is unknow when compiling, to another type, which is also unknow at compile time, in some function. One way is to use a vitual function and override that function with different arugment types. It's a heavy function with a lot of coding, so I want to search some other options. I'm wondering if there is a solution so that I only need to write one conversion function which can take care of whatever input/output types. Here are some pseudo codes which I'm not sure if they can be somehow realized. Hope it wouldn't confuse you. define type T*; Convert( T* in, T* out) { for_each_datum_in { *in=(type of out)*out; } } //call the function here Convert( TypeOfIn * in, TypeOfOut * out);

          now here am i

          N Offline
          N Offline
          Nuxser
          wrote on last edited by
          #4

          Must admit that I am not sure if I understood your problem. But according to I interpreted, when you talk about converting types, you imply the concept of "casting" (Casting one data type to be another). With respect to your given pseudocode, I guess you want the type pointed by "out" to be treated as type "in" so that the assignment can work nicely. Thus you want to cast the type pointed by "out" to be as type pointed by "in". If that understanding is true ... You need to delve in to understanding how "type casting" works in C++. A nice tutorial - http://www.cplusplus.com/doc/tutorial/typecasting.html I like it as they quickly goes into how type casting with classes/structs and pointer to those needs to be carefull than for basic types. Also for completeness, even in the case of template, it will eventually boils down to type casting as soon as the type is provided for the templates. So if you have defined two template types being them as type independent, during the runtime they are associated with provided types, hence the game boils down to type conversion between those two types. So understanding the "type casting" will enlighten you about how to do it and if done will it be meaning full. And for the conclusion, you will see that even though it is not a runtime error some casting will not be meaningfull or result in an wanted manner. There is no such freedom as converting ANY type to ANY type. Its not meaningfully possible (such as converting a student info class to a integer number). Hope it can help. Regards

          1 Reply Last reply
          0
          • W wallbrick

            I'm new to c++ and this forum, so please... I want to convert one type(int/float/double, can be 8/16/32/64bits), which is unknow when compiling, to another type, which is also unknow at compile time, in some function. One way is to use a vitual function and override that function with different arugment types. It's a heavy function with a lot of coding, so I want to search some other options. I'm wondering if there is a solution so that I only need to write one conversion function which can take care of whatever input/output types. Here are some pseudo codes which I'm not sure if they can be somehow realized. Hope it wouldn't confuse you. define type T*; Convert( T* in, T* out) { for_each_datum_in { *in=(type of out)*out; } } //call the function here Convert( TypeOfIn * in, TypeOfOut * out);

            now here am i

            N Offline
            N Offline
            Nuxser
            wrote on last edited by
            #5

            Just in case I wan't clear about it - I agree with Matthew Faithfull. And interestingly enough, quick Googling shows that there seems to be few implementation of Reflection in C++. http://www.extreme.indiana.edu/reflcpp/ Hey, Its C++, It can do everything ... ;)

            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