Re: Private method has been invokated as interface
Alex Vinokur wrote:
In program below a private method has been invokated as interface.
It is techically clear, but it is intuitively unclear.
No, it's perfectly clear.
The Base class in your example defines an interface and provides no
implementation (it uses the public pure virtual function to express this).
The Derived class provides an implementation (the body for the
overriding function), but has no public interface.
Taking them together, you have a working program.
The only thing that would be unintuitive in your example is this:
Base *p1 = new Derived();
Derived *p2 = new Derived();
p1->foo(); // OK
p2->foo(); // error!
This problem results from the fact that the Derived class modifies some
details of the interface which was already defined in the Base class.
In order to avoid this and making the distinction between the interface
and the implementation even more strict, here's the idiom that you can
find useful:
class Base
{
public:
void foo() { do_foo(); } // the interface
private:
virtual void do_foo() = 0; // no implementation
};
class Derived : public Base
{
public:
// no interface here!
private:
// the implementation:
virtual void do_foo() { /* ... */ }
};
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]