automatically calling a function when a variable changes ?

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

Ian Collins

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.
Wrap your members in a class that does the context checking for you?

Something like (rough and ready, untested):

template <typename T>
class Wrapper
{
T t;

public:

Wrapper( const T& t ) : t(t) {}

operator T() const { return t; }

operator=( const T& v ) { if (context) {t=v;} }
};

class TPoptions1
{
public:
TOptions1() {}
void setTemp1( int arg )
{
temp1 = arg;
}
void setTemp2( float arg )
{
temp2 = arg;
}
...
public:
Wrapper<int> temp1;
Wrapper<float> temp2;
};
 
P

Phlip

Ian said:
Wrap your members in a class that does the context checking for you?

Look up the Observer Pattern, then program to the interface not to the
implementation?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top