473,511 Members | 10,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

generic setVariable


Hi,

I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.

Thanks in advance

Kamran

Jul 27 '06 #1
7 1513
Kamran wrote:
I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
Sounds like it might be a design problem.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.
You could use a std::map of boost::any's (or boost::variant; cf.
http://boost.org):

class A
{
std::map<std::string, boost::anyprop_;
public:
template<typename T>
void SetVar( const std::string& s, const T& t )
{
prop_[ s ] = t;
}

// ...
};

Cheers! --M

Jul 27 '06 #2
Kamran wrote:
>
Hi,

I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.
The point of making member variables privat is to encapsulate them, i.e.
separate the interface from the impementation. If you want set/get
functions for every member variable, you can just as well make them public,
because you're exposing them anyway.

Jul 27 '06 #3

"Rolf Magnus" <ra******@t-online.dewrote in message
news:ea*************@news.t-online.com...
Kamran wrote:
>>
Hi,

I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.

The point of making member variables privat is to encapsulate them, i.e.
separate the interface from the impementation. If you want set/get
functions for every member variable, you can just as well make them
public,
because you're exposing them anyway.
Not always. It sounds like that's the case here, but I've had similar code
where there were side effects to setting the members, so I had to use setter
functions to change them. (Also, there are cases where the "variables" are
actually fields in a database hidden behind the class, but again, this case
doesn't sound like one of those cases.)

(As an aside, I really liked Delphi's feature of having "properties", which
looked to the calling code as if they were just members, but which could be
implemented via functions if desired. They also allowed for disabling
reading and/or writing the members. Very convenient, IMO.)

-Howard

Jul 27 '06 #4
mlimber wrote:
Kamran wrote:
>>I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.


Sounds like it might be a design problem.
Well, it is geophisical data and the number of parameters that can
change is huge. There is really not much I can do about that.
>
>>What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.


You could use a std::map of boost::any's (or boost::variant; cf.
http://boost.org):

class A
{
std::map<std::string, boost::anyprop_;
public:
template<typename T>
void SetVar( const std::string& s, const T& t )
{
prop_[ s ] = t;
}

// ...
};

Cheers! --M
Thanks for the tip. I'll have a look at that.

Kamran

Jul 27 '06 #5
Rolf Magnus wrote:
Kamran wrote:

>>Hi,

I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.


The point of making member variables privat is to encapsulate them, i.e.
separate the interface from the impementation. If you want set/get
functions for every member variable, you can just as well make them public,
because you're exposing them anyway.
These are not member variables that are part of internal class operation
but variables that a user should and could manipulate. Whether I define
them public or private and let a set of functions do the value
manipulation the amount of work is the same. I have to have a mechanism
to set their values to new ones. This is what I want to reduce (coding)
to have a more managable code. Lets say I have a class:

class A {
..
..
..
int var1;
float var2;
double var3;
Jul 27 '06 #6
Kamran wrote:
to set their values to new ones. This is what I want to reduce (coding)
to have a more managable code. Lets say I have a class:

class A {
.
.
.
int var1;
float var2;
double var3;
.
.
.
short var100;
.
.
.
};

How does one go changing those variable values, public or private
without doing that explicitally (if public) or through an interface ?
You can put only a set and a get function for each variable type, put any
type in an array, define an enumeration and use it to select the variable.
Something like:

enum IntVars { Var1 ... };
enum FloatVars { Var2 ... };
emum DoubleVars { Var3 .. };

int intvars [whatever];
int getVar (IntVars v);
{
return intvars [v];
}
void setVar (IntVars v, int value)
{
intvars [v]= value;
}

a.setVar (Var1, 1);

If the variables have meaningful names the code will be more legible than
this sample.

--
Salu2
Jul 27 '06 #7
Kamran schrieb:
>
Hi,

I have hundreds of variables inside a class (integer, float) and a long
range of set/get functions to manipulate their values.
What I am wondering about is whether there is a way in C++ to have
generic setVariable function which take a text string as a variable and
find the appropriate variable and either set or get its value.
I know that dynamic name resolution is not possible in C++ but is
there any other way to do this ? There are far too many variables
to do it individually one by one.

Thanks in advance

Kamran
You may put your attributes into a map and use the map to get the
references to your variables.

But this will
- break encapsulation, since the map doesn't know anything about private
oder protected members
- slow down your program, because lookup in the map will take
logarithmic time to access, where "real" member-access takes constant time.

If you really have hundreds of variables, which are also directly
accessibly via setters/getters, you might as well have a major design
problem. Look at your code and check if you really need all these
variables or if you can combine them into other classes.

Usually you don't need public access to all your attributes, if you do
need it, you may want to check whether you understood object-orientation....

Florian
Jul 30 '06 #8

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

Similar topics

1
1569
by: nick7001 | last post by:
Hi all, I am using the HTML_Template_IT package. I would like to use a template something like below. <!-- BEGIN TOPIC --> <h1>{TITLE} ....{TOPLINK}...</h1> {TOPIC-TEXT} <!-- END TOPIC -->
17
3290
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
1
1362
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
3
2749
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
9
12778
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
13
3794
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
7
2034
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
26
3591
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
4163
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
7245
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
7356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7512
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
5671
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
3227
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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 ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
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.