473,486 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

automatically calling a function when a variable changes ?

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
3 1328
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
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
Thank you guys, exactly what i needed... cheers

Jul 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2144
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
10
5920
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an...
8
2934
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
1
2036
by: orion30 | last post by:
I would like to know, if an allocated variable returning by a fuction is unallocated automatically ? If no how to proceed ? Best regards ...
30
2238
by: Tim Marshall | last post by:
Here's the scenario, A2003, Jet back end, illustrated with some cut down code at the end of the post: A proc dims a snapshot recordset (dim rst as Dao.recordset) and opens it. There are several...
5
3392
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
2572
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
8
12603
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below....
1
6214
by: billelev | last post by:
Here is some code that I have adapted slightly. It allows a report to be printed to a specific location. It works by calling SaveReportAsPDF and specifying the access report name, and the root...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.