"Deciding" in runtime the type of a variable

This is a discussion on "Deciding" in runtime the type of a variable within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if we could manually decide the type (and space) a variable could get. I mean using ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    "Deciding" in runtime the type of a variable

    Hi, I was wondering if we could manually decide the type (and space) a variable could get. I mean using a void pointer and assigning to that pointer the beggining address of any type we want, or class, etc... so we could use that variable for different things depending on what we want. Hope I'm being clear, I've got this code that works now but I need to know if I can get rid of the "switch":


    Code:
    void* pointer;
    int type_we_want;
    
    type_we_want = 2; //for example...
    
    switch(type_we_want)
    {
    	case 1:
    		pointer = new bool;
    		break;
    	case 2:
    		pointer = new char;
    		break;
    	case 3:
    		pointer = new short;
    		break;
    	case 4:
    		pointer = new int;
    		break;
    	//etc...
    }
    By getting rid of the switch, I mean I want to figure out if I could do something like this, though this doesn't work, as expected:

    Code:
    special_kind_of_var var[] = {bool, char, short, int};
    pointer = new  (var[type_we_want]);
    Get what I'm trying to do? I would like to know if it was possible to store TYPES only in an array, (by some special way) so that then we'd get in the array at the position representing the wanted type, so it could arrive to this:

    Code:
    void* pointer;
    nt type_we_want = 2;
    special_kind_of_var var[] = {bool, char, short, int};
    
    pointer = new (var[type_we_want]);
                                              //2
    pointer = new (var[2]);
                               //var[2]=short
    pointer = new short;
    I hope that "something" like this is possible, but if you can't see why I would do this, it could get as usefull as function pointers, when you have an application with tons of types, class, etc...

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The only thing I can think of is to pre-initialize this array with function pointers. The function pointers would then do the allocation for each type.

    edit:

    it would be like this... pointer = (*array_of_allocators[type_we_want]) ();
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Yeah, well that's what I had first began to do, but even if it works, it can makes up a lot of similar functions for nothing... Right now it's still what I do, but I don't know if it would be better with a switch, I just need to find what will be the fastest way to run my app. And a switch is also too much "case x:code;break;" for nothing...

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    RTTI may solve this. Some combination of dynamic_cast, typeid operator and the type_info class may work. But I'm afraid I'm not familiar with RTTI... yet.

    However, instead of an array you may consider a class that implements several data members with different types and an interface function returning a void* that will help you fill your void* pointer...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I'm feeling like whatever you find this useful for, there is probably a better solution. Templates are almost always the answer when it comes to undefined types during runtime. There is no real type of types, though. The easiest solution may very well be that switch statement.
    Last edited by SlyMaelstrom; 07-21-2006 at 04:11 PM.
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I would probably use a structure with two members: (1) an integer that defines the data type, and (2) a union of possible data types, somethind like below. Then you could do a switch on data.type

    Code:
    struct data
    {
      int type;
      union {
        char c;
        bool b;
        int i;
       long l;
       double d;
       float f;
       // add others here
    };

  7. #7
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Templates could have been a good solution but the problem is that my types don't have the same size so I don't think it would work ...

    dynamic_cast, typeid operator and the type_info, I don't really know about these things, but what is RTTI? Does it have to do with all these "dynamic_cast, typeid operator and the type_info"? I might have to do some google searches, but about "cast", it rings a little bell to me, it had to do with convertions, right?


    About unions, this is interesting, but the problem is that, even if I showed example of code that only feat. bool, char, short, and int, in fact I don't use any of them , I used only classes, so I don't believe I could use a union because some class will be small, some other big,... I can add that I do all this for a 2d game I'm making, and I have lots of differents objects so none of them is similar to others...
    Last edited by mikahell; 07-21-2006 at 04:22 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Get what I'm trying to do?
    Yes. Why are you trying to do it? Chances are good you're using the wrong language, or you really need to reconsider your design. C++ is pretty strongly typed, and that makes what you're trying to do much more awkward.
    My best code is written with the delete key.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by mikahell
    Templates could have been a good solution but the problem is that my types don't have the same size so I don't think it would work ...
    I don't see why not. Why don't you give me an example of what you're trying to do with your differently sized types? Otherwise, I was considering a union solution such as AD's, but I didn't know how to put it.
    Sent from my iPad®

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I am with Prelude, you are looking for scripting language functionality in a strongly typed compiled language. Why do you need a dynamic typing?

  11. #11
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, I need dynamic typing because that's the way I think it should work... But I know that C++ is not maybe the good choice for doing this, but I don't really want to try another language, I've been working with Flash before, I could do these types of things with it, but now I decided to move on to C++, because it's faster and it's harder, so that's the way I'm trying to learn things by myself...

    As for what I'm trying to do, it's really hard to explain, because everything's planned in my head, and being coded, but as I said, as it's for a game with different classes holding different number of variables, and of differents size, it gets complicated to deal in a union, (because of the fact that some class MAY need more variables than others, so I would get stuck with complicated too much things. But I'll have to try that someday because I do all what I do to learn things, so I will have to try unions, but even though I know how they work, I just can't find their use at the right moment...

    So I believe that there's not much thing that can be done without dynamic typing, but I'll have to make do with it...

    Thanks anyway...

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You're still really not explaining what you want to happen. You're just saying "There is big variables! AND LITTLE Ones! AND BIGGER LITTLER ONES! AND NONE OF IT WORKS!!! AHH!!!!"...

    ... just explain what you want to do. I have a feeling your answer lies within the realm of templates or polymorphism or both. Make an abstract base class that all your other classes derive from, then use your switch statement up there to allocate a base pointer with whatever one of your crazy derived classes you want.
    Last edited by SlyMaelstrom; 07-21-2006 at 05:35 PM.
    Sent from my iPad®

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Have you considered inheritance? That way you can have master classes with sub classes that can impliment the variables/functions it needs.

    [edit]pwnt by Sly[/code]

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Quote Originally Posted by SlyMaelstrom
    You're still really not explaining what you want to happen. You're just saying "There is big variables! AND LITTLE Ones! AND BIGGER LITTLER ONES! AND NONE OF IT WORKS!!! AHH!!!!"...
    I believe he explained what he wanted to do on his very first post!

    Regardless of whether he should do it or not, the question he posed is "how can I do it?".

    Now, if we do have an answer or think we have an answer that is good. Even if the answer means, like some already said it "don't do it in C++".
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Some good answers are alternative choices

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 6
    Last Post: 07-29-2008, 05:37 AM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  4. Replies: 6
    Last Post: 04-11-2008, 12:49 AM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21