Hi Marek,
(I apologise for the lengthy email, but you have touched upon something
that I have been thinking about.)
I guess Jurgen's reason here is to keep the object-oriented part of Ox
simple. Although he probably hasn't run into situations where he would
want public member variables, he has run into situations where he would
like private methods - at least that is how I interpret his prefacing
functions with an underscore in some of the packages that come with
Ox ;-P
I think there are often other good reasons for using methods rather than
accessing member variables directly - for example in something like the
Observer - Property design pattern, for e.g. something like lazy
evaluation of a formula. Suppose your class represents some complicated
function that takes a lot of time to calculate, and you change a
parameter. Doing this via a method (rather than accessing the member
directly) allows the class to know that one of its parameters has
changed. If you then request the value of the function, it knows it has
to recalculate. If you requested the value of the function without
having changed a parameter, the class would know that it doesn't have to
recalculate, and it would save you some expensive calculations. This is
superior to more messy ways of keeping track of what needs to be
recalculated.
You might also just want to validate input on SetX(somevalue) calls (e.
g. make sure that the matrix has the right dimensions etc).
Having said this, personally, I wouldn't mind a little bit of extra
choice and complexity by having private, public and protected member
variables (and methods!).
Note that if you want to save yourself a bit of typing, you could use
"polymorphic" properties like so:
class myclass
{
decl X;
X(...);
}
myclass::X(...)
{
args = va_arglist();
if(0==sizeof(args))
return X;
else
X = args[0];
}
You can then write myinstance.X() if you want to get the value, and
myinstance.X(value) if you want to set it. People do this e.g. in C++,
it just looks a little more clumsy in Ox because you can't define the
same method for different numbers of arguments.
My main gripe with the object-oriented bit of Ox actually is the lack of
being able to separate implementation inheritance from interface
inheritance (either multiple inheritance or "myclass(baseclass)
implements interface" as in Java). Personally, I feel this is more of a
problem than the fact that all member variables are private.
Max
On Tue, 2004-11-16 at 10:12 +0000, Marek Jarocinski wrote:
> Dear Jurgen,
>
> Often I find myself mechanically typing functions interfacing to member variables, like MyClass::GetX() and MyClass::SetX(const x), when I would prefer to access them directly by myclassobj.x
> What is the reason for making all member variables private? Have you considered relaxing this in the next version of Ox?
>
> Greetings,
>
> Marek
>
--
[log in to unmask] <[log in to unmask]>
|