Re: Restricting access should be illegal?
Daniel James wrote:
If the intention is to enforce a polymorphic interface callable only*
through the base class there are more rigid ways of doing so:
-----------------------
#include <stdio.h>
class A {
public:
void Member() { DoMemberStuff(); }
private:
virtual void DoMemberStuff() { printf("A::DoMemberStuff\n"); }
};
class B : public A {
private:
virtual void DoMemberStuff() { printf("B::DoMemberStuff\n"); }
};
int main()
{
A *a = new A();
a->Member(); // OK, A::Member calls A::DoMemberStuff
B *b = new B();
b->Member(); // OK, A::Member calls B::DoMemberStuff
A *a2 = b;
a2->Member(); // OK, A::Member calls B::DoMemberStuff
}
-------------------------
Now there is no confusion. The public interface in A is public and can
be called with the desired effect on objects of either type A or type
B; the internal implementation is polymorphic because DoMemberStuff is
virtual, and is private (in both classes) which is both appropriate
and consistent.
I can't immediately think of any reason not to do it this way, in real
code (I realize that Walter's snippet was just an example to expose
this quirk of the language).
I can think of a good reason: it is unnecessarily verbose and adds
cruft. You get no additional functionality for three times as much code.
Why should you mess around manually with copy and paste and regular
expressions when the language offers an automated, idiomatic way (which
you call a quirk)?
Template Method is a valuable pattern when the template method does some
processing of its own, such as implementing Design by Contract. For
simple callback interfaces, as in Observer, this is typically not
needed, and maintaining do-nothing wrappers just for the sake of
rigidity and not confusing Java programmers would be grotesque.
It is ironic that languages which do not allow changing the access
level, such as Java and C#, also prohibit implementation in interfaces,
which renders the solution you advocate impossible.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]