generic 'variable' class

Aug 31, 2009 at 3:15am
Im writing a generic variable class ie:

1
2
3
4
5
6
7
variable a = "Hello";
variable b = 'a';
variable c = 34;
/* ... */
a = 34;
b = "Hello";
c = 'a';


.. everything is going smoothly. however, I am looking for some opinions on overloaded operator behaviors. I was thinking maybe subtracting two 'string' types of variable could perform STL's remove() or some other useful thing... example:

1
2
3
variable c = "Hello Everyone";
variable m = "e";
variable New = c-m; // "Hllo Evryon" 


or anything along those lines..

note the variable class is not a template class, only its constructor:

1
2
3
template <class T>
  inline variable(T __t)
    { this-> __set(__t); }


and overloaded operators use templates...
Last edited on Aug 31, 2009 at 2:01pm
Aug 31, 2009 at 3:45am
Trust me. You don't want to do this.
Last edited on Aug 31, 2009 at 3:45am
Aug 31, 2009 at 4:16am
You just reinvented boost::any.

www.boost.org
Aug 31, 2009 at 1:59pm
it was just for practice
can boost::any change type dynamically?
Last edited on Aug 31, 2009 at 2:00pm
Aug 31, 2009 at 2:20pm
1
2
3
4
boost::any a = 5;
a = 3.14;
a = std::vector<int>( 4 );
a = "Hello world";


works just fine.

The trick is that in order to get the value back out of the boost::any, you have to know the type it is holding.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
boost::any a = 3.14;

try {
    int x = boost::any_cast<int>( a );
    std::cout << "a is integral, and holds value " << x << std::endl;
} catch( const boost::bad_any_cast& ) {
    std::cout << "a does not hold an integer!";
}

try {
    double d = boost::any_cast<int>( a );
    std::cout << "a is floating point (double), and holds value " << d << std::endl;
} catch( const boost::bad_any_cast& ) {
    std::cout << "a does not hold a double!";
}


would print*


a does not hold an integer!
a is floating point (double), and holds value 3.14


*For illustration only; actual value output may vary (scientific notation, etc).
Aug 31, 2009 at 2:36pm
i never heard about boost anything... looks ez tho. -_-
Last edited on Aug 31, 2009 at 2:36pm
Topic archived. No new replies allowed.