Initializing a subset of state

Discussion in 'C++' started by Dave, Mar 2, 2005.

  1. Dave

    Dave Guest

    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
     
    Dave, Mar 2, 2005
    #1
    1. Advertising

  2. Dave

    Guest

    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
     
    , Mar 3, 2005
    #2
    1. Advertising

  3. 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
     
    Jonathan Turkanis, Mar 3, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guoqi Zheng

    subset of data using dataview??

    Guoqi Zheng, Jan 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,034
    Miha Markic
    Jan 19, 2004
  2. Andy
    Replies:
    0
    Views:
    433
  3. news.microsoft.com

    validators subset

    news.microsoft.com, May 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Peter Blum
    May 27, 2004
  4. =?Utf-8?B?S2V2aW4gQmFua3M=?=

    Deploying a subset of an asp.Net app

    =?Utf-8?B?S2V2aW4gQmFua3M=?=, Oct 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    387
    =?Utf-8?B?S2V2aW4gQmFua3M=?=
    Oct 21, 2004
  5. =?Utf-8?B?QW5kcmU=?=

    Forms-Based Authentication for only a subset of pages

    =?Utf-8?B?QW5kcmU=?=, Dec 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    395
    =?Utf-8?B?QW5kcmU=?=
    Dec 30, 2004
Loading...

Share This Page