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. Type Variable

Type Variable

Scheduled Pinned Locked Moved C / C++ / MFC
wpfhelpquestion
12 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.
  • A Offline
    A Offline
    Aidman
    wrote on last edited by
    #1

    Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:

    VARTYPE Type;

    Type = int; // The type is integer.

    switch (Type) {
    case short: // The type is short.
    //...
    break;
    case int: // The type is integer.
    //...
    break;
    case float: // The type is float.
    //...
    break;
    case double: // The type is double.
    //...
    break;
    }

    I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

    I I M N 4 Replies Last reply
    0
    • A Aidman

      Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:

      VARTYPE Type;

      Type = int; // The type is integer.

      switch (Type) {
      case short: // The type is short.
      //...
      break;
      case int: // The type is integer.
      //...
      break;
      case float: // The type is float.
      //...
      break;
      case double: // The type is double.
      //...
      break;
      }

      I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      almost: switch(sizeof(type)) { case sizeof(short): //need pre-defined break; includeh10

      I 1 Reply Last reply
      0
      • I includeh10

        almost: switch(sizeof(type)) { case sizeof(short): //need pre-defined break; includeh10

        I Offline
        I Offline
        Ian Darling
        wrote on last edited by
        #3

        In most 32 bit C++ compilers, float and int are both sizeof 4, so how's that going to work? -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

        1 Reply Last reply
        0
        • A Aidman

          Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:

          VARTYPE Type;

          Type = int; // The type is integer.

          switch (Type) {
          case short: // The type is short.
          //...
          break;
          case int: // The type is integer.
          //...
          break;
          case float: // The type is float.
          //...
          break;
          case double: // The type is double.
          //...
          break;
          }

          I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

          I Offline
          I Offline
          Ian Darling
          wrote on last edited by
          #4

          Off the top of my head, I believe some of Andrei Alexandrescu's work (Loki??) covers situations like this, although I don't have a reference to hand. Also, the VARIANT struct already has some associated defines that could be used as a base for this - eg VT_I4, VT_UI1, VT_BSTR, etc... -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

          N 1 Reply Last reply
          0
          • A Aidman

            Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:

            VARTYPE Type;

            Type = int; // The type is integer.

            switch (Type) {
            case short: // The type is short.
            //...
            break;
            case int: // The type is integer.
            //...
            break;
            case float: // The type is float.
            //...
            break;
            case double: // The type is double.
            //...
            break;
            }

            I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            Look up typeid and the type_info struct --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

            I 1 Reply Last reply
            0
            • M Michael Dunn

              Look up typeid and the type_info struct --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

              I Offline
              I Offline
              Ian Darling
              wrote on last edited by
              #6

              I forgot about those :-O Obviously been too long since I did any major work in C++. Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? /goes into a bunker with a C++ compiler and a copy of Stroustrup. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

              M 1 Reply Last reply
              0
              • I Ian Darling

                I forgot about those :-O Obviously been too long since I did any major work in C++. Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? /goes into a bunker with a C++ compiler and a copy of Stroustrup. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                Ian Darling wrote: Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

                I 1 Reply Last reply
                0
                • M Michael Dunn

                  Ian Darling wrote: Of course, IIRC, typeid and type_info are platform dependant, so code isn't necessarily portable, right? Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

                  I Offline
                  I Offline
                  Ian Darling
                  wrote on last edited by
                  #8

                  Michael Dunn wrote: Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. Nope. Just that you couldn't rely on certain things about the type_info implementation always being the same across platforms, for example, what typeinfo::name() returns. My reading of "Design and Evolution of C++" suggests that there is scope for differing definitions of the type_info class on different C++ platforms. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                  M 1 Reply Last reply
                  0
                  • I Ian Darling

                    Michael Dunn wrote: Not sure what you mean... Comparing type_infos generated on two different platforms probably won't work, if that's what you had in mind. Nope. Just that you couldn't rely on certain things about the type_info implementation always being the same across platforms, for example, what typeinfo::name() returns. My reading of "Design and Evolution of C++" suggests that there is scope for differing definitions of the type_info class on different C++ platforms. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    Ok, I see. Honestly, I've never used type_info so I'm just repeating the docs here. ;) --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

                    A 1 Reply Last reply
                    0
                    • M Michael Dunn

                      Ok, I see. Honestly, I've never used type_info so I'm just repeating the docs here. ;) --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber CP SearchBar v2.0.2 released

                      A Offline
                      A Offline
                      Aidman
                      wrote on last edited by
                      #10

                      Thanks! Althougth I am not quite sure if I might be able to use it as I hoped, it is still a better alternative then declaring your own Type-Ids. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

                      1 Reply Last reply
                      0
                      • A Aidman

                        Hi, I am in need of a variable that can hold a type (variable-type ex: int or double) and not a value of the specific type, just the type. Something that works like this:

                        VARTYPE Type;

                        Type = int; // The type is integer.

                        switch (Type) {
                        case short: // The type is short.
                        //...
                        break;
                        case int: // The type is integer.
                        //...
                        break;
                        case float: // The type is float.
                        //...
                        break;
                        case double: // The type is double.
                        //...
                        break;
                        }

                        I know I can simply declare a bunch of constant numbers, who each represents a variable type. But that would be very non-optimal if there already exists an easier way. Could I use templates to achieve this or the use of preprocessors/macros? Just to be clear, I am looking for a variable that only contains a type and not a value. Any help or tips are most appreciated. :) Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.

                        N Offline
                        N Offline
                        Neville Franks
                        wrote on last edited by
                        #11

                        Boost Any may be of interest: http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                        1 Reply Last reply
                        0
                        • I Ian Darling

                          Off the top of my head, I believe some of Andrei Alexandrescu's work (Loki??) covers situations like this, although I don't have a reference to hand. Also, the VARIANT struct already has some associated defines that could be used as a base for this - eg VT_I4, VT_UI1, VT_BSTR, etc... -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

                          N Offline
                          N Offline
                          Neville Franks
                          wrote on last edited by
                          #12

                          There is also Boost Any which may be of interest: http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                          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