Selectors and modificators trouble

Discussion in 'C++' started by Krivenok Dmitry, Sep 13, 2005.

  1. I have class:
    .....
    class Config
    {
    ....
    private:
    int max_users;
    int max_proc;
    std::string cfg_file_name;
    ....
    };
    .....
    For each private member of class I should write pair (selector and
    modificator):

    int GetMaxUsers() const;
    void SetMaxUsers(int value);

    It's not very good for me (10 private members)!!!
    What method exists for solving this problem?
    Krivenok Dmitry, Sep 13, 2005
    #1
    1. Advertising

  2. Krivenok Dmitry

    mlimber Guest

    Krivenok Dmitry wrote:
    > I have class:
    > ....
    > class Config
    > {
    > ....
    > private:
    > int max_users;
    > int max_proc;
    > std::string cfg_file_name;
    > ....
    > };
    > ....
    > For each private member of class I should write pair (selector and
    > modificator):
    >
    > int GetMaxUsers() const;
    > void SetMaxUsers(int value);
    >
    > It's not very good for me (10 private members)!!!
    > What method exists for solving this problem?


    The technique of Get/Set methods allows you to encapsulate the data and
    hide the implementation of your class. That way, if in the future, you
    need to change the way, say, max_users is calculated, you can do so,
    without forcing the user to change his/her code.

    The Get/Set methods can be inlined so there is no calling overhead, and
    the minor inconvenience of writing twenty, one-line methods is usually
    out-weighed by the gains of separating of interface and implementation.

    Alternately, you can make the members public or give friend access to
    the necessary functions and classes. These approaches, however, are
    usually inferior since they violate encapsulation and force the user to
    rely on your class implementation rather than a more abstract
    interface.

    Cheers! --M
    mlimber, Sep 13, 2005
    #2
    1. Advertising

  3. Krivenok Dmitry

    Zara Guest

    Krivenok Dmitry wrote:
    > I have class:
    > ....
    > class Config
    > {
    > ....
    > private:
    > int max_users;
    > int max_proc;
    > std::string cfg_file_name;
    > ....
    > };
    > ....
    > For each private member of class I should write pair (selector and
    > modificator):
    >
    > int GetMaxUsers() const;
    > void SetMaxUsers(int value);
    >
    > It's not very good for me (10 private members)!!!
    > What method exists for solving this problem?
    >


    In the strict case you are holding a configuration, as the name of the
    class suggests, it may be easier and better to use two maps:

    std::map<std::string,std::string> for std::string values and
    std::map<std::string,int> for int values.

    That way it should be easy to write and read them to/from anywhere they
    are stored, to use something as get(std::string name) as the way to get
    their values, etc.

    And it is automatic to extend the number of properties stored, and it is
    very easy to create default values...

    If it is a config you are managing, it is better to treat it as a congi
    map than as a config object. And make it a Singleton.

    Zara
    Zara, Sep 13, 2005
    #3
  4. * Krivenok Dmitry:
    > I have class:
    > ....
    > class Config
    > {
    > ....
    > private:
    > int max_users;
    > int max_proc;
    > std::string cfg_file_name;
    > ....
    > };
    > ....
    > For each private member of class I should write pair (selector and
    > modificator):
    >
    > int GetMaxUsers() const;
    > void SetMaxUsers(int value);
    >
    > It's not very good for me (10 private members)!!!
    > What method exists for solving this problem?


    It seems that all a Config instance does is to keep track of a bunch of
    values. Consider using a collection object.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 13, 2005
    #4
  5. Krivenok Dmitry

    Howard Guest

    "Krivenok Dmitry" <> wrote in message
    news:...
    >I have class:
    > ....
    > class Config
    > {
    > ....
    > private:
    > int max_users;
    > int max_proc;
    > std::string cfg_file_name;
    > ....
    > };
    > ....
    > For each private member of class I should write pair (selector and
    > modificator):


    "Modificator?" :) I think the more common term is "accessor functions".
    Also used are "getters and setters" (from the fact their names are preceded
    with "Get" and "Set").

    >
    > int GetMaxUsers() const;
    > void SetMaxUsers(int value);
    >
    > It's not very good for me (10 private members)!!!
    > What method exists for solving this problem?
    >


    Ten is really not that many.

    Do you need setters for all of them? Most times, I've found that only one
    or two members need later modification, once the object is constructed. So
    I often have several getters and only one or two setters.

    Another idea: if some of the members would generally be changed
    simultaneously, you could have one or two setter functions that take more
    than one parameter.

    But, you might also consider whether you really need to keep the data
    private in the first place. Do you have other users of your class, and the
    need to keep control over how the internals are handled? If not, then you
    might consider just making the data public.


    -Howard
    Howard, Sep 13, 2005
    #5
    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. tshad

    IE on attribute selectors

    tshad, Nov 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    331
    tshad
    Nov 12, 2004
  2. Matt Beckwith

    Class and ID Selectors (CSS question)

    Matt Beckwith, Jul 12, 2003, in forum: HTML
    Replies:
    3
    Views:
    371
    David Dorward
    Jul 13, 2003
  3. Luigi Donatello Asero

    Class selector, ID selectors and link pseudo-classes

    Luigi Donatello Asero, Jun 4, 2005, in forum: HTML
    Replies:
    4
    Views:
    506
    Luigi Donatello Asero
    Jun 4, 2005
  4. Luigi Donatello Asero
    Replies:
    1
    Views:
    395
    Luigi Donatello Asero
    Jun 4, 2005
  5. Replies:
    3
    Views:
    109
Loading...

Share This Page