A
alan
I'm creating a sort-of "wrapper" class which (partly) acts like a
variable. Something like:
template<class t>
class cell{
t curval;
public:
/*public for debugging only - will be private in final version*/
inline cell<t>& set_value(t v){ curval = v; return *this;}
inline t get_value(){ return curval;}
/*actual public interface*/
/*assign to this value*/
inline cell<t>& operator=(t v){ return set_value(v);}
};
....
int
main(void){
cell<int> v;
v = 0;
cout << "v = " << ((int)v);
return 0;
}
However I can't figure out how to make the compiler do v.get_value()
when v is used in an int() context. Since this is templated, the only
conversion I want to support is something like cell<type> -> type.
Attempts to convert to unrelated types should signal an error.
I can't figure out the correct syntax to do this, unfortunately
(should probably look for better manuals, sigh).
----
In any case the actual target would be something like this:
int
main(void){
cell<int> v;
cell<int> v2;
v = v2 + 1; //v automatically changes when v2 is changed
v2 = 3;
if((int)v == 4){
cout << "Test 1 passed\n";
} else{
cout << "Test 1 failed\n";
return 1;
}
v2 = 4;
if((int)v == 5){
cout << "Test 2 passed\n";
} else{
cout << "Test 2 failed\n";
return 1;
}
return 0;
}
Basically the cell<> class would be like a spreadsheet cell - when I
assign a formula into it, its value will be automatically updated if
any of the cells in the formula are updated.
The way I intend to implement it is, I extend +-*/ and ?: so that if
any cells are in any parameters, they will instead return a new cell
which registers itself to the input cells. Something like:
template<class t>
cell<t>& operator+(cell<t> v1, t v2){
sum_cell<t> *sum = new sum_cell<t>(v2);
sum.register_yourself_to(v1); //when v1 is update()d, sum's update()
method is called
return (cell<t>&) *sum;
}
If there is already some existing library that does (in some form) the
above, please inform me. I suspect there already is, somehow I have a
feeling I've seen it before.
variable. Something like:
template<class t>
class cell{
t curval;
public:
/*public for debugging only - will be private in final version*/
inline cell<t>& set_value(t v){ curval = v; return *this;}
inline t get_value(){ return curval;}
/*actual public interface*/
/*assign to this value*/
inline cell<t>& operator=(t v){ return set_value(v);}
};
....
int
main(void){
cell<int> v;
v = 0;
cout << "v = " << ((int)v);
return 0;
}
However I can't figure out how to make the compiler do v.get_value()
when v is used in an int() context. Since this is templated, the only
conversion I want to support is something like cell<type> -> type.
Attempts to convert to unrelated types should signal an error.
I can't figure out the correct syntax to do this, unfortunately
(should probably look for better manuals, sigh).
----
In any case the actual target would be something like this:
int
main(void){
cell<int> v;
cell<int> v2;
v = v2 + 1; //v automatically changes when v2 is changed
v2 = 3;
if((int)v == 4){
cout << "Test 1 passed\n";
} else{
cout << "Test 1 failed\n";
return 1;
}
v2 = 4;
if((int)v == 5){
cout << "Test 2 passed\n";
} else{
cout << "Test 2 failed\n";
return 1;
}
return 0;
}
Basically the cell<> class would be like a spreadsheet cell - when I
assign a formula into it, its value will be automatically updated if
any of the cells in the formula are updated.
The way I intend to implement it is, I extend +-*/ and ?: so that if
any cells are in any parameters, they will instead return a new cell
which registers itself to the input cells. Something like:
template<class t>
cell<t>& operator+(cell<t> v1, t v2){
sum_cell<t> *sum = new sum_cell<t>(v2);
sum.register_yourself_to(v1); //when v1 is update()d, sum's update()
method is called
return (cell<t>&) *sum;
}
If there is already some existing library that does (in some form) the
above, please inform me. I suspect there already is, somehow I have a
feeling I've seen it before.