473,587 Members | 2,516 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 1522
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::s tring, boost::anyprop_ ;
public:
template<typena me 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::s tring, boost::anyprop_ ;
public:
template<typena me 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
1578
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
3306
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
1
1371
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 it to be able to take in an instance of the generic REGARDLESS of what the "Of" type of the generic is. E.g. .... given a generic class ...
3
2753
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 dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from...
9
12813
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 implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
13
3809
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The...
7
2036
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
26
3604
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even...
2
4173
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
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...
0
5395
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...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.