[c++] Looking for a way to create a "uni variable"

Samael88

Evil always finds a way
Reaction score
181
As the title says, I am looking for a way to create an "uni variable".

kind of that works like this:
Code:
int integer = 0;
char character = a;
uni univar[2];

univar[0] = integer;
univar[1] = character;

I don't believe that there is a variable which I can do this with, but it would be nice to know a way to do so, any suggestions?
 

Samael88

Evil always finds a way
Reaction score
181
So you want to create a variable of variables? o_O

Not really, just a way to put different types of variables in the same list or array^^

I could put them into a 2d char array, but I wanted to know if there is an easier way to do it first^^

I need it so that I can make a system to easier save and load data from files.
I want to be able to just point to the necessary variables and then the system does the rest with as few commands as possible:D
 

Romek

Super Moderator
Reaction score
963
Use the 'object' type instead?
Click (.NET)
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Not really, just a way to put different types of variables in the same list or array^^

I could put them into a 2d char array, but I wanted to know if there is an easier way to do it first^^

I need it so that I can make a system to easier save and load data from files.
I want to be able to just point to the necessary variables and then the system does the rest with as few commands as possible:D

I don't know if there's a native way, but...

Code:
class Uni {};

class Int extends Uni {

    private:
        int which

    public:
        Int(char whichInt) {which = whichInt;}

};

class Char extends Uni{

    private:
        char which

    public:
        Char(char whichChar) {which = whichChar;}

};

Char character = Character('b');
Int integer = Int(4);
Uni univar[2];

univar[0] = character;
univar[1] = integer;

Seems like it would be the proper method of doing it.
 
Reaction score
333
The simplest solution is to use a tagged union.

Code:
struct Uni {
   union {
      char charValue;
      int intValue;
      bool boolValue;
   };

   enum {
      CHAR,
      BOOL,
      INT,
   } tag;
};

/* 

Uni uni;

uni.tag = Uni::BOOL;
uni.boolValue = true;

uni.tag = Uni::INT;
uni.intValue = 42;

*/
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top