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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Dunamic type converstion. Please help?

Dunamic type converstion. Please help?

Scheduled Pinned Locked Moved Managed C++/CLI
helpdata-structuresjsonquestion
8 Posts 3 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.
  • L Offline
    L Offline
    LiquidE_SA
    wrote on last edited by
    #1

    I've got a problem. I'll get input to my program and I have to determine which type of data that is. Input will occur in a text file and will look similar to this: Add: 41.4 51.8 48.4 22.1 Delete: 84.1 57.4 15.1 etc.... The problem is I have to determine the type of the data that needs to be added to the tree. The tree is a template class. I want to know code that could do the same as the following is supposed to do: typedef T; T = int of T = double; AbstractTree *tree = new BTree; I then want to use type T in the rest of my program. etc. AbstractClass *tree = new BTree; I need this for a project that needs to be in on Monday 20:00(GMT) Your help will be greatly appreciated. Thanks alot

    V 1 Reply Last reply
    0
    • L LiquidE_SA

      I've got a problem. I'll get input to my program and I have to determine which type of data that is. Input will occur in a text file and will look similar to this: Add: 41.4 51.8 48.4 22.1 Delete: 84.1 57.4 15.1 etc.... The problem is I have to determine the type of the data that needs to be added to the tree. The tree is a template class. I want to know code that could do the same as the following is supposed to do: typedef T; T = int of T = double; AbstractTree *tree = new BTree; I then want to use type T in the rest of my program. etc. AbstractClass *tree = new BTree; I need this for a project that needs to be in on Monday 20:00(GMT) Your help will be greatly appreciated. Thanks alot

      V Offline
      V Offline
      VaporTrace
      wrote on last edited by
      #2

      You need something to parse your input with like a char str and extract your numerical into a double. int doesn't do decimal so how do you know if a decimal is in your input? It's a text file input so it's automatically a char going in. atof() and isalnum() would be my first thought or get out your ASCII table if you want to build your own smarter atof() to parse your entire input line at a time. I don't know of anyone that wants to just hand you an answer to copy. Remember the basic purpose of a template it to reduce redundant code differing only in parameter type, allowing the same call with different types. But then again maybe I'm not understanding what you are doing, like are you pulling the entire input file into a buffer or are you grabing input by keyword, space, line ??

      L 1 Reply Last reply
      0
      • V VaporTrace

        You need something to parse your input with like a char str and extract your numerical into a double. int doesn't do decimal so how do you know if a decimal is in your input? It's a text file input so it's automatically a char going in. atof() and isalnum() would be my first thought or get out your ASCII table if you want to build your own smarter atof() to parse your entire input line at a time. I don't know of anyone that wants to just hand you an answer to copy. Remember the basic purpose of a template it to reduce redundant code differing only in parameter type, allowing the same call with different types. But then again maybe I'm not understanding what you are doing, like are you pulling the entire input file into a buffer or are you grabing input by keyword, space, line ??

        L Offline
        L Offline
        LiquidE_SA
        wrote on last edited by
        #3

        I'm grabbing input word by word. instead of coding: if (input is numeric) if (input contains '.'){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } else { AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } if (input is char*){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } I want to do something similar to: typename int T; AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); I'm trying to find out if there's a way of changing the type of T at run-time

        V R 2 Replies Last reply
        0
        • L LiquidE_SA

          I'm grabbing input word by word. instead of coding: if (input is numeric) if (input contains '.'){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } else { AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } if (input is char*){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } I want to do something similar to: typename int T; AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); I'm trying to find out if there's a way of changing the type of T at run-time

          V Offline
          V Offline
          VaporTrace
          wrote on last edited by
          #4

          So you're trying to ring my bell because now you are creating ambigious function pointers.

          1 Reply Last reply
          0
          • L LiquidE_SA

            I'm grabbing input word by word. instead of coding: if (input is numeric) if (input contains '.'){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } else { AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } if (input is char*){ AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); } I want to do something similar to: typename int T; AbrstrackTree *p = new BTree(); AbrstrackTree *p = new BStarTree(); AbrstrackTree *p = new BPlusTree(); AbrstrackTree *p = new Trie(); I'm trying to find out if there's a way of changing the type of T at run-time

            R Offline
            R Offline
            RichardS
            wrote on last edited by
            #5

            As far as I can remember once a type is defined that's it. The compiler does the conversion from T to int/double/long/etc. So at run-time there is no T, it as become a known integral type so you can't change it. Also your code above is really strange and does not make much sense. But I really don't get what the real problem is. If you define T as double (as per VaporTrace) then you can set it as an integer or decimal from your input. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

            L 1 Reply Last reply
            0
            • R RichardS

              As far as I can remember once a type is defined that's it. The compiler does the conversion from T to int/double/long/etc. So at run-time there is no T, it as become a known integral type so you can't change it. Also your code above is really strange and does not make much sense. But I really don't get what the real problem is. If you define T as double (as per VaporTrace) then you can set it as an integer or decimal from your input. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

              L Offline
              L Offline
              LiquidE_SA
              wrote on last edited by
              #6

              The problem is that the input I an getting can be of any basic data type....int, double,char..etc My data-structure is using templates that is been made at compile-time, but I need the type to change in real time. I think there is a way to do it using a derived class of the template class, but I can't seem to figure it out. I'm sure many people had the same problem before with something like using a STL vector or list, and not knowing what type of data will be stored in the vector, at compile time. Please help!! I hope I explained my problem clearly.....?

              R 1 Reply Last reply
              0
              • L LiquidE_SA

                The problem is that the input I an getting can be of any basic data type....int, double,char..etc My data-structure is using templates that is been made at compile-time, but I need the type to change in real time. I think there is a way to do it using a derived class of the template class, but I can't seem to figure it out. I'm sure many people had the same problem before with something like using a STL vector or list, and not knowing what type of data will be stored in the vector, at compile time. Please help!! I hope I explained my problem clearly.....?

                R Offline
                R Offline
                RichardS
                wrote on last edited by
                #7

                Hi, Ok, the way you could do it is by creating a union. Seeing the you need types int, double and char, just create a union of them all:

                typedef union
                {
                char c;
                double d;
                int i;
                } UTYPES;

                This way you can use that for your template class. Now which ever type you need it will be in the union. But remember to be careful how you treat the union. You will also have to do some nifty programming in the template class to get around not being an exact type. The best would be a container class for it so that you can have some control over what values are stored in the union. Hope this helps. :) regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                L 1 Reply Last reply
                0
                • R RichardS

                  Hi, Ok, the way you could do it is by creating a union. Seeing the you need types int, double and char, just create a union of them all:

                  typedef union
                  {
                  char c;
                  double d;
                  int i;
                  } UTYPES;

                  This way you can use that for your template class. Now which ever type you need it will be in the union. But remember to be careful how you treat the union. You will also have to do some nifty programming in the template class to get around not being an exact type. The best would be a container class for it so that you can have some control over what values are stored in the union. Hope this helps. :) regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                  L Offline
                  L Offline
                  LiquidE_SA
                  wrote on last edited by
                  #8

                  We'll try that. Thanks alot for your help.

                  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