473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Selectors and modificators trouble

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?

Sep 13 '05 #1
4 1505
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

Sep 13 '05 #2
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
Sep 13 '05 #3
* 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?
Sep 13 '05 #4

"Krivenok Dmitry" <di**@inotech.ru> wrote in message
news:11**********************@g43g2000cwa.googlegr 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
Sep 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tony Benham | last post by:
I have been getting to grips with css recently (very slowly), and one area I have a problem is when to use class selectors or id selectors. Are there any guidelines when to use each type ? The key...
11
by: Sue Sims | last post by:
From <URL: http://www.w3.org/TR/CSS21/selector.html#id-selectors > I find the following text: "What makes attributes of type ID special is that no two such attributes can have the same...
18
by: news.pandora.be | last post by:
I tried everything. I even tested it in combination with opera and mozilla. (I use IE 6 mostly) It's reduced to the simple example below. Please tell me what is wrong with my understaning of...
16
by: Stoic | last post by:
Hi, I'm relatively new to CSS. I'm trying to get all the urls in a group of text bracketed by <p></p> tags to be underlined and everywhere else to NOT be underlined. As of now my code is like...
4
by: Andy Fish | last post by:
hi, I am using descendent selectors to format cells within a table according to the css class of the table. However, when using nested tables, it seems to pick up the outer matching rule rather...
7
by: Biguana | last post by:
Hi, Apologies in advance for a newbie question, but I'm trying to decipher css stuff, I can't find a direct parallel in web tutorials, and I'm not sure what to search on. My problem is that...
6
by: Rhino | last post by:
What's the simplest way to determine which, if any, of my selectors are not needed in a given stylesheet? I have a small number of HTML pages that share two stylesheets; one stylesheet is for...
7
by: Dan | last post by:
Hi, IE 7 does not seem to like child selectors for lists. I couldn't find anything addressing this with google search. Example of problem-- If I want to apply the color red to all ordered list...
7
by: John Dann | last post by:
I'm unclear as to how best to use what I'm terming the top-level CSS selectors, by which I mean selectors like *, html and body. I'm coming at this from trying to understand how best to set font...
5
by: Nathan Sokalski | last post by:
I'm not sure if this is the right place to ask this question, but I wasn't sure where else to go. I have a table made of the following tags: <table class="myclass"> <tbody> <tr><td>.</td></tr>...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.