By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

automatically calling a function when a variable changes ?

P: n/a
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

Jul 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ma*****@yahoo.com wrote:
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<inttemp1;
Wrapper<floattemp2;
};

--
Ian Collins.
Jul 2 '06 #2

P: n/a
Ian Collins wrote:
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?

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 2 '06 #3

P: n/a
Thank you guys, exactly what i needed... cheers

Jul 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.