Hello all,
I have a class that contains a large number of discrete pieces of state
information. Any combination of these member variables might be valid for a
given object. Any given member variable might be used or unused in a given
object. For each field, there is a FieldIsValid() member function that must
be called before accessing or mutating a given field. If a field is
reported as not valid, it must not be accessed or mutated.
Let me take a quick detour here. I think this is a terrible design, but
it's what I've been tasked to work with. I am the maintainer, not the
original implementer. This class is heavily used, so extricating it and
replacing it with something better designed is not an option. OK, detour
over.
Constructors have been defined for the combinations of fields that have been
needed so far. Now, more combinations of fields are needed. The number of
constructors is liable to grow exponentially. Rather than continuing to
tack on additional constructors in this manner, I would like to come up with
a general scheme to initialize any subset of member variables. Note that I
said "initialize", not "assign". The object needs to be in a usable state
after construction. It is not acceptable to construct it half-baked and
then go call a bunch of mutators for the fields I care about.
I have had some thoughts on this, but I would also like to solicit
suggestions. Any and all suggestions will be appreciated!
Thanks,
Dave 2 1493
Dave wrote: Hello all,
I have a class that contains a large number of discrete pieces of
state information. Any combination of these member variables might be
valid for a given object. Any given member variable might be used or unused in a
given object. For each field, there is a FieldIsValid() member function
that must be called before accessing or mutating a given field. If a field is reported as not valid, it must not be accessed or mutated.
This is a really terrible idea. It is perfectly okay to have a
constructor that initializes a subset of the object's value, but every
contructor should construct the object "having default values for
unspecified fields."
Let me take a quick detour here. I think this is a terrible design,
but it's what I've been tasked to work with. I am the maintainer, not
the original implementer. This class is heavily used, so extricating it
and replacing it with something better designed is not an option. OK,
detour over.
Okay.
Constructors have been defined for the combinations of fields that
have been needed so far. Now, more combinations of fields are needed. The
number of constructors is liable to grow exponentially. Rather than continuing
to tack on additional constructors in this manner, I would like to come
up with a general scheme to initialize any subset of member variables.
One straightforward method is to default-construct the object "in a
valid, but unspecified state," (meaning each field has some valid
default value), and then use a "set" manipulator to set each data
member. Initializing a subset of the object's value becomes a matter of
calling the appropriate subset of manipulators. This is more typing,
but it is scalable. If your class has value-semantics, you need all
these manipulators anyway to have any hope of writing reasonable test
cases for 'operator==' and 'operator!='.
Note that I said "initialize", not "assign". The object needs to be in a usable
state after construction.
If you cannot default-construct the object in a valid state, you cannot
do what you want to do very easily.
That said, another method is to provide a variant data type conforming
to a schema known to the object. You specify the data object and the
schema, and the object constructor initializes the appropriate data
members. Schemas can be generated automatically by the build system.
The constructor can even use a utility function to initialize the data
members through assignment via the manipulators. But at this point,
you're just adding unneccessary complexity.
It is not acceptable to construct it half-baked and then go call a bunch of mutators for the fields I care about.
Can you provide a technical reason why this is not feasible?
/david
Dave wrote: Hello all,
I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member variable might be used or unused in a given object. For each field, there is a FieldIsValid() member function that must be called before accessing or mutating a given field. If a field is reported as not valid, it must not be accessed or mutated.
Boost will soon has a named parameters library which will allow you to constuct
an object like so:
Thing t(height = 6, width =12.3, name = "stanley",
favorite_movie="splash");
Here the keywords height, weight, .. are labels which can be specified in any
order.
Unfortunately I can't find a link to the documentation.
Thanks, Dave
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dan Perl |
last post by:
There is something with initializing mutable class attributes that I am
struggling with. I'll use an example to explain:
class Father:
attr1=None # this is OK
attr2= # this is wrong...
|
by: Chris Dutrow |
last post by:
I searched around on the net for a bit, couldn't find anything though.
I would like to find some code for a function where I input A Range Of
Integers
For example: Function( 1, 100 );
And the...
|
by: Robert Vazan |
last post by:
I am looking for other people's attempts to create safe subset of C and
enforce it with scripts. Does anybody know about anything like this?
By "safe", I mean the following:
* Strongly typed...
|
by: JSheble |
last post by:
I'm writing some code that interacts with MSMQ, and am getting in the habit
of using try{}catch{}finally{} blocks, but am now running into a problem
with un-initialized variables... for example,...
|
by: alacrite |
last post by:
If I have this situation
class X
{
Z z;
Y y;
};
Class X has two objects of type Z and Y. How do I initialize z and y
with non default constructors?
|
by: Jay Tee |
last post by:
Hi,
I have some code that does, essentially, the following:
- gather information on tens of thousands of items (in this case, jobs
running on a
compute cluster)
- store the information as a...
|
by: junky_fellow |
last post by:
Guys,
I was just looking at some code where to initialize an integer
with all F's following statement is used;
unsigned int i = -1;
I want to know if this is the right way of doing the...
|
by: Jason Doucette |
last post by:
Situation:
I have a simple struct that, say, holds a color (R, G, and B). I
created my own constructors to ease its creation. As a result, I lose
the default constructor. I dislike this, but...
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
| |