473,609 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ 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.setTemp 1( 1 );
context = 0;
options.setTemp 2( 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 1332
ma*****@yahoo.c om 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<inttemp 1;
Wrapper<floatte mp2;
};

--
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
2155
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 containting a template for a tuple, the function then finds a tuple that matches this template and then returns it. The template may contain variable names, which match against anything in that position in the tuple - this is all working fine,...
10
5946
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 ASP page which produces a fixed width text file for import into a third party legacy application which I don't have control over. --code sample-- Dim strSomeString
8
2952
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 p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
1
2045
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 ------------------------------------------------------------------------ In my case, I want to return aa. After the call of polit, is aa is
30
2267
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 nested do loops, going through the records in rst using .movenext. At one point in one of the loops, we'll say the rst is at record "a". Now, another subprocedure is called, passing rst to it. In the subprocedure, the recordset goes through a...
5
3414
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), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
5
2591
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 fine so far. My problem is that when I call a web method to modify the dataset inside the Web service it is never updated and I get no errors. currentPeriods method will always give me 1 even if I call ChangeDataset and call currentPeriods...
8
12613
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. The code below draws an error as indicated. Surely there has to be a better way than to make xxx a session variable? Thanks
1
6242
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 directory to which the filename should be saved. The routine adjusts registry values to achieve its aim. Now, it works, but not quite as I would like it. I originally thought that I would be able to adjust a registry key (such as...
0
8139
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8408
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6064
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5524
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1403
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.