Re: Private method has been invokated as interface
Martin Bonner wrote:
[snip example of declaring a public virtual in a base class, and then
overriding it with a private member in a derived class].
I am sorry, but I don't understand what your point is.
Yes, it is legal. No, it is not a good idea. So don't do that.
I disagree. It is a perfectly natural technique for implementing private
callback interfaces (although derivation is usually private in this case).
class Publisher
{
public:
void Subscribe (Subscriber* s);
// unsubscribing left out for brevity
};
class Subscriber
{
public:
virtual void Notify (Message m) = 0;
};
class MySubscriber : private Subscriber
{
public:
MySubscriber (Publisher p) { p.Subscribe (this); }
// rest of public interface
private:
// this is not intended for MySubscriber's clients
virtual void Notify (Message m);
};
Do you have a better way to implement this relationship and express the
intent of the design? In languages like Java or C#, this is impossible
because access level is tied to overriding. What is really annoying
about these languages is that they also prohibit you from circumventing
the restriction like this (C++):
class Subscriber
{
public:
void Notify (Message m) { this->DoNotify (m); }
private:
virtual void DoNotify (Message m) = 0;
};
because of another restriction that does not allow implementation in
interfaces.
--
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! ]