[LC++]Names of sets and gets

Dr Mark H Phillips mark at austrics.com.au
Mon Jan 19 07:19:05 UTC 2004


On Mon, 2004-01-19 at 01:54, Peter Poulsen wrote:
> Hi
> 
> I have been wondering if it would give more or less clear code to call
> the sets and gets the same as the variables. E.g.:

Generally my practice is to use

  int x() const {return _x;}

(as you have done --- but note the missing const in yours) for gets
and to do

  void xput(int t) {_x = t;}

for sets.

I use "put" rather than "set" because the latter is also used to
refer to a collection of things and I wish to retain this usage.

I don't just overload x(...) when doing sets because I want to
emphasize the fact that a "dangerous operation" (a modification of
the object) is being performed.  Also, often several things will
need changing at once when modifying an object, in which case I
just do something like:

  void expandput(int x_, double, y_) ...

Cheers,

Mark.

> class A
> {
> public:
> 	void x(int t) { _x = t; }
> 	int x() { return _x; }
> private:
> 	int _x;
> };
> 
> I know this purely affects how the code looks, not how it works. What is
> your opinion?
> 




More information about the tuxCPProgramming mailing list