Re: duct typing and interface in C++
On Jul 24, 7:23 pm, TP <Tribulati...@Paralleles.invalid> wrote:
Alain Ketterlin wrote:
class Daffy : public InterfaceDuck
Inheritance must be explicit for the types to be compatible.
In fact, I would like to have duck typing exactly as in
Python: in Python there is no need to have an inheritance
relation between the interface and the class Daffy. In my
previous post, I pointed to an example with no such
inheritance relation:
http://stackoverflow.com/questions/289176/how-is-duck-typing-differen...
the-old-variant-type-and-or-interfaces
I would like to have a construction where the following
affectation is possible:
InterfaceDuck * d = new Daffy;
without inheritance relation between InterfaceDuck and Daffy.
The fact that Daffy has a method Quack() as InterfaceDuck
should be enough to allow that.
But perhaps there is no means to do that in C++?
There isn't. Python and C++ are designed to fulfill different
roles. Duck typing would make C++ inappropriate for many of the
roles it now fills. Duck typing means, for example, only
finding out about errors at runtime. No big deal in a small
program, written in a language without a compiler, in an
interactive development environment, and where robustness isn't
paramount. Not having duck typing means having to go back and
add the proper base classes every time you extend a class to
support a new protocol. Not appropriate in an experimental and
highly dynamic environment, and overkill (and a lot of extra
work) in very small, frequently changing programs.
--
James Kanze