Nov 22, 2013 at 12:28pm Nov 22, 2013 at 12:28pm UTC
i'm building the variant type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <cstdlib>
#include <iostream>
#include <string>
#ifndef VARIANT_H_INCLUDED
#define VARIANT_H_INCLUDED
class variant
{
string a="" ;
public :
variant (string value="" )
{
a=value;
}
variant (double value)
{
a=to_string(value);
}
friend istream& operator >>(istream &is,variant &obj)
{
is>>obj.a;
return is;
}
friend ostream& operator <<(ostream &os,const variant &obj)
{
os<<obj.a;
return os;
}
friend istream &getline(istream &in, variant &s1)
{
getline(in, s1.a);
return in;
}
variant & operator = (int const & b)
{
a=to_string(b);
return *this ;
}
variant & operator = (string const & b)
{
a=b;
return *this ;
}
variant & operator = (double const & b)
{
a=to_string(b);
return *this ;
}
variant & operator = (float const & b)
{
a=to_string(b);
return *this ;
}
bool operator == (string const & b)
{
return (a==b);
}
operator string() const
{
return a; // return string member
}
operator double () const
{
return atof(a.c_str()) ;
}
};
#endif // VARIANT_H_INCLUDED
but don't accept class\struct\enum or others.
i think that i can use the:
void * b;
but if theres so many types and we can create a new types, how can i prepare the 'b' for reacive the values?
Last edited on Nov 22, 2013 at 12:29pm Nov 22, 2013 at 12:29pm UTC
Nov 22, 2013 at 12:37pm Nov 22, 2013 at 12:37pm UTC
It's common to create a variant from a Union with a Tag that identifies what field of the union is being used.
You've used a string as the implementation, presumably to convert that content to/from the externally exposed type.
Nov 22, 2013 at 12:41pm Nov 22, 2013 at 12:41pm UTC
even using a Union, how you can prepare for a class's\struct\enum and others?
Nov 22, 2013 at 12:57pm Nov 22, 2013 at 12:57pm UTC
let me ask you more 1 thing: using templates, i must overload the operators too(+,- and others)?
Nov 22, 2013 at 1:18pm Nov 22, 2013 at 1:18pm UTC
thanks for all.. thanks to all