Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Selectors and modificators trouble

Reply
Thread Tools

Selectors and modificators trouble

 
 
Krivenok Dmitry
Guest
Posts: n/a
 
      09-13-2005
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?

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      09-13-2005
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

 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      09-13-2005
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-13-2005
* 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?
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-13-2005

"Krivenok Dmitry" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sibling selectors and reordering elements eefacm@gmail.com Javascript 3 03-26-2008 01:00 PM
Re: Class selector, ID selectors and link pseudo-classes Luigi Donatello Asero HTML 1 06-04-2005 06:58 PM
Class selector, ID selectors and link pseudo-classes Luigi Donatello Asero HTML 4 06-04-2005 05:02 PM
IE on attribute selectors tshad ASP .Net 1 11-12-2004 11:05 PM
Class and ID Selectors (CSS question) Matt Beckwith HTML 3 07-13-2003 08:39 PM



Advertisments