Variable-type variable?

This is a discussion on Variable-type variable? within the C++ Programming forums, part of the General Programming Boards category; Hi, I am in need of a variable that can contain any variable type. Something like this: Code: VARTYPE Var; ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy Variable-type variable?

    Hi,

    I am in need of a variable that can contain any variable type. Something like this:
    Code:
    VARTYPE Var;
    
    Var = int;
    Var = double;
    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?

    Any help or tips are highly appreciated.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A union would perhaps be the best solution here:
    Code:
    typedef union
    {
       int iVal;
       short int sVal;
       long int lVal;
       char cVal;
       float fVal;
       double dVal;
    }MultiVal;
    
    int main()
    {
       MultiVal MyVal = {0};
       MyVal.iVal = 65;
       cout << MyVal.cVal << endl; //Prints the ASCII character for 65, which is 'A'
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    hmm... ok, sorry I might have been a bit unspecific.

    What I want is a variable that only holds a variable-type (like: int or double) and not its value. For ex:
    Code:
    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;
    }
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I don't think you would be able to use the actual type names, those are kind of reserved. I'd do what Magos said, seems like the best way.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Maybe you could use a void pointer??

    Code:
    void *Type;
    
    Type = new int(0);
    switch(sizeof(*Type)){
    case 1: //char
    case 2: //short
    etc..
    Just a suggestion, i've never tried it...

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    That's a nice idea, but aren't some variable types the same size as each other? Like, signed/unsigned etc.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Yes, they are, but you could make a crude check..

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think some context would help here. I find it hard to imagine the purpose of the code, so I can't really think of any solutions.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,705
    Missing javascript, are we?

    Here's one way:

    Code:
    struct variable
    {
      enum var_type
     {
      unassigned,
      string_type,
      double_type
     };
     
     var_type type;
     string string_rep;
     double double_rep;
    
     variable & operator += (const char * str)
     {
       if(type == unassigned)
        type = string_type;
       if(type == string_type)
      {
       string_rep += str;
      }
       else if(type == double_type)
      {
       double_rep += strtod(str, 0);
      }
      return *this;
     }
     variable & operator += (double val)
     {
       if(type == unassigned)
        type = double_type;
       if(type == string_type)
      {
       char str[16];
       sprintf(str, "%f", val);
       string_rep += str;
      }
       else if(type == double_type)
      {
       double_rep += val;
      }
      return *this;
     }
     variable & operator = (const char * str)
     {
       if(type == unassigned)
        type = string_type;
       if(type == string_type)
      {
       string_rep = str;
      }
       else if(type == double_type)
      {
       double_rep = strtod(str, 0);
      }
      return *this;
     }
     variable & operator = (double val)
     {
       if(type == unassigned)
        type = double_type;
       if(type == string_type)
      {
       char str[16];
       sprintf(str, "%f", val);
       string_rep = str;
      }
       else if(type == double_type)
      {
       double_rep = val;
      }
      return *this;
     }
     variable & operator += (variable & val)
     {
      if(val.type == string_type)
      {
       return *this += val.string_rep.c_str();
      }
      else if(val.type == double_type)
      {
       return *this += val.double_rep;
      }
      return *this;
     }
     variable & operator = (variable & val)
     {
      if(val.type == string_type)
      {
       return *this = val.string_rep.c_str();
      }
      else if(val.type == double_type)
      {
       return *this = val.double_rep;
      }
      return *this;
     }
     variable(const char * str)
      :type(string_type)
     {
      *this = str;
     }
     variable(double val)
      :type(double_type)
     {
      *this = val;
     }
     variable()
     :type(unassigned), double_rep(0), string_rep("")
    {
    
    }
    friend ostream & operator << (ostream & out, variable & _this)
    {
       if(_this.type == variable::string_type)
      {
       out << _this.string_rep;
      }
       else if(_this.type == variable::double_type)
      {
       out << _this.double_rep;
      }
    
     return out;
    }
    friend istream & operator >> (istream & in, variable & _this)
    {
       if(_this.type == variable::string_type)
      {
       in >> _this.string_rep;
      }
       else if(_this.type == variable::double_type)
      {
       in >> _this.double_rep;
      }
    
     return in;
    }
    };







    Code:
    int main()
    {
     variable str = "This is ";
     variable num = 1;
    
     str += num;
     str += " way to do it!";
     cout << str << endl;
     
     str = "255";
     num += str;
     cout << num << endl;
    
     cin.get();
    }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So basically, the idea behind this is to overload the operators for each type we want to look at? That way, we can set the variable type, and subsequent operations can be dealt with accordingly, right?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Thanks for your replys but it seems like there ain't no better way than making a list of all the variable types and representing each of them with their own unique "Id" number. Life can be crule some times
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So what kind of program was this code part of? I'm curious as to why you'd need to implement something like this.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    C++, by design, is much more type-safe than many other languages. There are ways of getting around this (unions and void* come to mind), but in general, it is a bad idea to try to circumvent the type-safety. Indeed, if you tell us your application for this, we may be able to help you come up with a better design.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 08:51 AM
  5. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM

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