Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8217
| Date | 2011-07-25 23:36 +0200 |
|---|---|
| From | Werner Erasmus <werasm@gmail.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: duct typing and interface in C++ |
| References | <ivqtf8-ke1.ln1@rama.nodalpoint.universe> |
| Message-ID | <Ivadnf-zaaFGfLDTnZ2dnUVZ8kmdnZ2d@saix.net> (permalink) |
TP wrote:
> Hello,
>
> I convert a Python/Qt program (PyQt) in C++/Qt.
> The programmer has used the duck typing of Python, thus I would like to have
> duck typing in C++, in fact exactly what is described in the "best answer"
> at the following address:
>
> http://stackoverflow.com/questions/289176/how-is-duck-typing-different-from-
> the-old-variant-type-and-or-interfaces
>
> (the "best answer" is the second post of the page, the one with a green
> sign).
>
> Is it possible in C++?
>
> Thanks in advance,
>
> TP
I've tried just for fun. This is probably a naive
imitation... I just used codepad therefore had
no other libraries (e.g. boost available).
Output:
Duffy quacked!
Donald quacked!
//code...
#include <iostream>
#include <algorithm>
class Duck
{
struct QuackIF
{
virtual void quack() = 0;
virtual QuackIF* clone() const = 0;
virtual ~QuackIF(){}
};
template <class T>
struct QuackImpl : QuackIF
{
typedef void (T::*QuackFunc)();
QuackImpl( T& receiver )
: receiver_( &receiver ),
f_( &T::quack )
{
}
virtual QuackIF* clone() const
{
return new QuackImpl( *this );
}
virtual void quack()
{
(receiver_->*f_)();//Quack!!!
}
virtual ~QuackImpl(){}
private:
T* receiver_;
QuackFunc f_;
};
QuackIF* duck_;
public:
Duck( const Duck& other )
: duck_( other.duck_ ? other.duck_->clone() : 0 )
{
}
~Duck(){ delete duck_; }
Duck& operator=( Duck other )
{
std::swap( other.duck_, duck_ ); return *this;
}
template <class DuckT>
Duck( DuckT* duck )
: duck_( new QuackImpl<DuckT>( *duck ) )
{
}
void quack()
{
if( duck_ ){ duck_->quack(); }
else
{
std::cout << "Quack!" << std::endl;
}
}
};
struct Daffy
{
void quack(){ std::cout << "Duffy quacked!" << std::endl; }
};
struct Donald
{
void quack(){ std::cout << "Donald quacked!" << std::endl; }
};
int main()
{
Duck duck = new Daffy;
duck.quack();
duck = new Donald;
duck.quack();
}
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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