Looking for a better way of getting/monitoring data

There is a bit of data that other objects need to look at but don't need to modify. So I created public const references to the private data as a way of getting this info. It looks like this:

1
2
3
4
5
6
7
8
9
class A{
public: 
A():var_c(var){}

const int &var_c;

private: 
int var;
};


However, some objects may need to continually monitor info like this and don't have the luxury of knowing during construction what it will be monitoring and don't have the luxury of always knowing what it is monitoring during its update phase.

So, when some piece of data needs to be monitored a pointer within the monitoring class is hooked up to it
. The problem is this requires a const_cast call every time it needs to monitor different data sets.


Is there a better way of dealing with this whole scenario?
If 'var' needs to be accessed by function inside the class you don't need anything at all.
If it needs to be accessed by few other functions, you can make them friends you class A.
If it has to be accessed by many functions (external to your class) you should make get/set functions for 'var'
In this case I'm looking just at your 3rd situation, where it needs to be accessed by objects external to the class.

I was using Get for a bit, and then had to switch them to at least & (in the cases of classes that had to monitor data continually / complex objects), then decided to be const safe since only the class holding the data is allowed to modify it.

So in the end I would end up with something like:
const int &GetVar(){ return var; }

This still leaves me with the problem of classes that need to monitor this variable and hold it internally. While const_cast is an option, it seems like a shady shortcut to a solution. These get's are meant to return read-only r-values, but I cannot figure out a way to deal with it without turning it into a possible l-value.

Heres an example of the specific scenario:
Lets say we have objA and objB. A special situation occurs which objA is triggered to monitor var of objB - objA.monitor( objB.getvar() ). Now the special situation no longer occurs but objA must still process and monitor the var of objB for changes in data.

The original solution was to create a pointer to var within objB, but to do this with const correctness would require the use of const_cast and making the original const data an l-value, which I would prefer to keep as an r-value only.

It is this sort of situation where I am having problems with and looking for a solution for.
If you want other classes to have a copy of the variable, those classes should contain a const int* const (assuming you don't want to change which int it is pointing too). You will have to intialize this in the constructor as well if you make the pointer const.
That's the other issue...the monitoring class triggers under a specific conditions and does not know ahead of time what data it will be monitoring (other than the type). It will often switch from/to different sets of data as well.
Then just make it a const int*
thanks!
Topic archived. No new replies allowed.