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 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
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.
"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
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
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;
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 -->
|
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...
|
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...
|
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...
|
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...
| |
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...
|
by: Dave |
last post by:
I've got these declarations:
public delegate void FormDisplayResultsDelegate<Type>(Type
displayResultsValue);
public FormDisplayResultsDelegate<stringdisplayMsgDelegate;
instantiation:...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |