M
mast2as
hi guys
I wonder if there's a way of doing something like that in C++ which is
more elegant then the "basic" approach. I have lets say a class
TOptions1 which has a few member variables. It is possible to change
the value of these variables but they should only change if the context
in which they are called allows it.
Here is an example
class TPoptions1
{
public:
TOptions1() {}
void setTemp1( int arg )
{
if ( context == 1 ) {
temp1 = arg;
}
}
void setTemp2( float arg )
{
if ( context == 1 ) {
temp2 = arg;
}
}
...
public:
int temp1;
float temp2;
};
int context = 1;
....
TOptions options;
options.setTemp1( 1 );
context = 0;
options.setTemp2( 2.0 );
okay so this is lets say one way of doing it. My problem is that I have
several classes that need to do the same thing (checking the context
before the values can be eventually changed) and there's also quite a
few member variables. So at the end duplicating the same code over and
over to do a context checking doesn't seem to be so efficient.
I know computers can't think by themselves yet, but is there a
mechanism or pattern in C++ where something can be executed
automatically (like a function) when the program tries to change the
value of a class member variable ?
Doing something like that
options.temp1 = 1; would automatically call some over function that
could eventually do some context checking (where the functions called
could also be different based on the class).
i am just asking... ignore the post if the question sounds stupid to
you ;-) but if there's a good solution to this problem i'd be happy to
hear about it.
thank you, mark
I wonder if there's a way of doing something like that in C++ which is
more elegant then the "basic" approach. I have lets say a class
TOptions1 which has a few member variables. It is possible to change
the value of these variables but they should only change if the context
in which they are called allows it.
Here is an example
class TPoptions1
{
public:
TOptions1() {}
void setTemp1( int arg )
{
if ( context == 1 ) {
temp1 = arg;
}
}
void setTemp2( float arg )
{
if ( context == 1 ) {
temp2 = arg;
}
}
...
public:
int temp1;
float temp2;
};
int context = 1;
....
TOptions options;
options.setTemp1( 1 );
context = 0;
options.setTemp2( 2.0 );
okay so this is lets say one way of doing it. My problem is that I have
several classes that need to do the same thing (checking the context
before the values can be eventually changed) and there's also quite a
few member variables. So at the end duplicating the same code over and
over to do a context checking doesn't seem to be so efficient.
I know computers can't think by themselves yet, but is there a
mechanism or pattern in C++ where something can be executed
automatically (like a function) when the program tries to change the
value of a class member variable ?
Doing something like that
options.temp1 = 1; would automatically call some over function that
could eventually do some context checking (where the functions called
could also be different based on the class).
i am just asking... ignore the post if the question sounds stupid to
you ;-) but if there's a good solution to this problem i'd be happy to
hear about it.
thank you, mark