Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8155

Re: duct typing and interface in C++

From Nobody <nobody@nowhere.com>
Subject Re: duct typing and interface in C++
Date 2011-07-24 21:21 +0100
Message-Id <pan.2011.07.24.20.21.55.211000@nowhere.com>
Newsgroups comp.lang.c++
References <ivqtf8-ke1.ln1@rama.nodalpoint.universe> <pan.2011.07.24.16.39.52.289000@nowhere.com> <bu2uf8-lkq.ln1@rama.nodalpoint.universe> <871uxfzgzp.fsf@dpt-info.u-strasbg.fr> <0j4uf8-a9j.ln1@rama.nodalpoint.universe>
Organization Zen Internet

Show all headers | View raw


On Sun, 24 Jul 2011 20:23:59 +0200, TP wrote:

> In fact, I would like to have duck typing exactly as in Python:

Then use Python.

> in Python 
> there is no need to have an inheritance relation between the interface and 
> the class Daffy.

That's because Python is a dynamically-typed language, where method
look-up is done at run time.

> In my previous post, I pointed to an example with no such inheritance 
> relation:
> 
> http://stackoverflow.com/questions/289176/how-is-duck-typing-different-from-
> the-old-variant-type-and-or-interfaces

Note that the code given there isn't valid C++.

> 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++?

Well, not unless you consider using C++ to write a dynamically-typed
language such as Python to be a solution.

If, for some reason, you can't make specific classes derive from
InterfaceDuck, you can use a template to create a wrapper or proxy for
such classes, e.g.:

template <class T>
class ProxyDuck : public InterfaceDuck {
	T& ref;
	ProxyDuck(T& ref) : ref(ref) {}
	void quack() { ref.quack(); }
};

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-24 17:40 +0200
  Re: duct typing and interface in C++ Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-07-24 18:11 +0200
    Re: duct typing and interface in C++ Marcel Müller <news.5.maazl@spamgourmet.org> - 2011-07-25 00:03 +0200
      Re: duct typing and interface in C++ Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-07-25 10:11 +0200
      Re: duct typing and interface in C++ Goran <goran.pusic@gmail.com> - 2011-07-25 04:18 -0700
  Re: duct typing and interface in C++ Nobody <nobody@nowhere.com> - 2011-07-24 17:39 +0100
    Re: duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-24 19:55 +0200
      Re: duct typing and interface in C++ Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-07-24 20:11 +0200
        Re: duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-24 20:23 +0200
          Re: duct typing and interface in C++ Dombo <dombo@disposable.invalid> - 2011-07-24 20:45 +0200
          Re: duct typing and interface in C++ Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-07-24 21:10 +0200
          Re: duct typing and interface in C++ Nobody <nobody@nowhere.com> - 2011-07-24 21:21 +0100
            Re: duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-24 23:50 +0200
              Re: duct typing and interface in C++ Stuart Redmann <DerTopper@web.de> - 2011-07-24 23:33 -0700
                Re: duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-25 23:00 +0200
              Re: duct typing and interface in C++ Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-25 01:35 -0500
              Re: duct typing and interface in C++ Goran <goran.pusic@gmail.com> - 2011-07-25 04:10 -0700
          Re: duct typing and interface in C++ James Kanze <james.kanze@gmail.com> - 2011-07-30 11:51 -0700
      Re: duct typing and interface in C++ Dombo <dombo@disposable.invalid> - 2011-07-24 20:21 +0200
        Re: duct typing and interface in C++ Dombo <dombo@disposable.invalid> - 2011-07-24 20:26 +0200
  Re: duct typing and interface in C++ Dombo <dombo@disposable.invalid> - 2011-07-24 18:36 +0200
    Re: duct typing and interface in C++ BGB <cr88192@hotmail.com> - 2011-07-24 10:02 -0700
  Re: duct typing and interface in C++ BGB <cr88192@hotmail.com> - 2011-07-24 09:45 -0700
    Re: duct typing and interface in C++ TP <Tribulations@Paralleles.invalid> - 2011-07-24 20:15 +0200
      Re: duct typing and interface in C++ BGB <cr88192@hotmail.com> - 2011-07-24 14:06 -0700
  Re: duct typing and interface in C++ Thomas Boell <tboell@domain.invalid> - 2011-07-25 02:05 +0200
  Re: duct typing and interface in C++ Qi <no@no.no> - 2011-07-25 09:54 +0800
  Re: duct typing and interface in C++ Werner Erasmus <werasm@gmail.com> - 2011-07-25 23:36 +0200
    Re: duct typing and interface in C++ Leigh Johnston <leigh@i42.co.uk> - 2011-07-25 23:22 +0100
      Re: duct typing and interface in C++ Werner Erasmus <werasm@gmail.com> - 2011-07-26 08:35 +0200

csiph-web