Selectors and modificators trouble

K

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?
 
M

mlimber

Krivenok said:
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
 
Z

Zara

Krivenok said:
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
 
A

Alf P. Steinbach

* 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.
 
H

Howard

Krivenok Dmitry said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top