Re: Kinds of methods
- From: "andrew queisser" <andrewdotqueisser@xxxxxx>
- Date: Wed, 22 Mar 2006 18:29:40 GMT
What about threads? Just now I'm working on a class representing a server
and it has a member function that's running as a thread. I guess it's really
a modifier since it never returns to the caller but does change the external
state of the object.
Andrew
"Daniel T." <postmaster@xxxxxxxxxxxxx> wrote in message
news:postmaster-6FCD75.09250822032006@xxxxxxxxxxxxxxxxxxxxxxxxxx
I just wrote this up for someone on one of the C++ newsgroups. Any
comments?
Member-functions can be categorized into one of 4 types:
modifier - a member-function that can change the external state of the
object. IE calling a modifier could cause one or more other
member-functions of the object's class to behave differently or
produce different output when called on this object, than they
otherwise would before the modifier was called.
producer - a member-function that creates an object for the caller to
use, or sets the state of an object the caller passed in.
accessor - a member-function that gives the caller a pointer/reference
to one of the object's member-variables such that modifying the value
contained in the pointer/reference will change the state of the
object as if a modifier was called.
provider - a member-function that gives to the caller a
pointer/reference to one of the object's member-variables such that
modifying the value contained in the pointer/reference will not
change the state of the object that the member-function was called on.
In C++, we also have a special purpose modifier member-function...
destructor - a designated "last call". Class designers, can be assured
that this member-function will be the last one called on an object,
it will be called exactly once, and that it will be called for all
objects.
Note that I didn't reference constructors. The reason is because,
strictly speaking, in C++ a constructor is not called on an object, but
on an uninitialized block of memory. Some other languages treat
constructors differently.
It is possible for a single member-function to be placed in more than
one category, for example a modifier/producer. Generally, I frown on
such member-functions, preferring that each member-function "do one
thing well". However, sometimes it makes sense for a member-function to
return the results of the modifier call.
Of the 4:
Accessors should generally be avoided because they provide a "back
door" way of changing the state of the object. In C++ the problem can be
mitigated by using the 'const' qualifier thus disallowing untoward
changes to the object's state. However, they still restrict the ways in
which the entire class can be changed so should only be used if a
producer member-function is too slow.
Providers generally only exist in container type classes, logically
objects of the class don't own the object's they provide.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
.
- References:
- Kinds of methods
- From: Daniel T.
- Kinds of methods
- Prev by Date: Re: Kinds of methods
- Next by Date: Re: Kinds of methods
- Previous by thread: Re: Kinds of methods
- Next by thread: Re: Kinds of methods
- Index(es):
Relevant Pages
|