Re: Kinds of methods
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Thu, 23 Mar 2006 12:29:20 +0100
On Wed, 22 Mar 2006 14:25:14 GMT, Daniel T. wrote:
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.
This is not a member in C++. Member is a dispatching (virtual) subroutine.
C++ does not support function virtual in the result. Other languages do.
[...]
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.
Destructor is different in the way of its composition. Methods are composed
out of non-overlapping pieces [overridings.] Destructor is composed
incrementally. Each new "overriding" contains all previous as a part.
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.
This is no matter. Constructor has an output parameter of the object's
type. So it is a polymorphic subprogram.
The real difference to normal methods is that both constructor and
destructor are type mutators. They change the type of the parameter:
destructor takes T and makes void of it, constructor takes void and makes
T.
Accessors should generally be avoided because they provide a "back
door" way of changing the state of the object.
That depends on the state. If the object state is a Cartesian product of
the substates changed by accessors, then it is OK. Example: container. Then
the accessor may return an ADT acting as a smart pointer, which would
maintain object's invariant. Example: array slices.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- References:
- Kinds of methods
- From: Daniel T.
- Kinds of methods
- Prev by Date: Re: multimethod + multiple inheritance
- Next by Date: Re: multimethod + multiple inheritance
- Previous by thread: Re: Kinds of methods
- Next by thread: UML-ish question
- Index(es):
Relevant Pages
|