Getting module of a class
bearophile
bearophileHUGS at lycos.com
Fri Oct 17 11:55:56 PDT 2008
Andrei Alexandrescu:
> Don wrote:
> > No. The goal is to provide better encapsulation.
> > There's a paper by Scott Meyers about it.
>
> A great one:
> http://www.drdobbs.com/cpp/184401197
> This goes straight to the discussion I was trying to avoid the other
> day. People still have dogmatic views of OO...
Very nice article, written to the point. I did read it a lot of time ago, but now I have understood it.
D has modules, so they can be used to contain the free functions related to a class.
This page is less clear for me, but I presume in D there are ways to tell what T is:
http://www.drdobbs.com/showArticle.jhtml?documentID=cuj0002meyers&pgno=2
>Such interfaces allow class clients to do anything they might reasonably want to do, but classes contain no more member functions than are absolutely necessary.<
There are no absolutes. You can perform some operation on a class both using those core member functions, or accessing fields more directly. Sometimes it happens that doing it using only the core member functions gives a slower code. Adding some non minimal methods may speed up your code. The author says the same thing few lines below:
>Of course, a minimal class interface is not necessarily the best interface. I remarked in Effective C++ that adding functions beyond those truly necessary may be justifiable if it significantly improves the performance of the class, makes the class easier to use, or prevents likely client errors<
Problems:
1) The asymmetry in the calling way between those free functions and the methods increases the complexity of the class, because for each function or method the programmer (or the IDE) has to remember an extra bit, that tells if it's a member or not. Free functions "happen" as the article says, but that's extra complexity that you are willing to pay if you want to add some exotic functionality; while you aren't ready to pay such price even if you want to use the class in a standard way (as it often happens if its API is well designed).
2) If you create a derived class it can't re-define some of those methods, because some of them are free functions, not virtual methods. In a dynamic language this isn't a problem, but in static brittle language this may give problems. I am not sure.
Bye,
bearophile
More information about the Digitalmars-d
mailing list