Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > duct typing and interface in C++

Reply
Thread Tools

duct typing and interface in C++

 
 
TP
Guest
Posts: n/a
 
      07-24-2011
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/2...ifferent-from-
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++?

TP
 
Reply With Quote
 
 
 
 
Dombo
Guest
Posts: n/a
 
      07-24-2011
Op 24-Jul-11 20:21, Dombo schreef:
> Op 24-Jul-11 19:55, TP schreef:
>> Nobody wrote:
>>
>>> On Sun, 24 Jul 2011 17:40:02 +0200, TP wrote:
>>>
>>>> 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++,
>>>
>>>> Is it possible in C++?
>>>
>>> Duck typing is just interfaces, which are just classes with no member
>>> variables and only pure virtual methods.

>>
>> I have tried to convert the example of the webpage I mentioned:
>>
>> #include<iostream>
>>
>> class InterfaceDuck
>> {
>> public:
>> virtual void Quack() = 0;
>> };
>>
>> class Daffy
>> {
>> public:
>> void Quack()
>> {
>> std::cout<< "coin coin"<< std::endl;
>> }
>> };
>>
>> int main( void )
>> {
>> InterfaceDuck * d = new Daffy;
>> d->Quack();
>>
>> delete d;
>>
>> return 0;
>> }
>>
>> At the compilation, I obtain:
>> $ g++ -Wall test.cpp
>> test.cpp: In function ?int main()?:
>> test.cpp:21:21: error: cannot convert ?Daffy*? to ?InterfaceDuck*? in
>> initialization
>>
>> Now, if I replace the line:
>> InterfaceDuck * d = new Daffy;
>> by:
>> InterfaceDuck * d = (InterfaceDuck *) new Daffy;
>>
>> It compiles without warning, but it segfaults at the execution.
>> So, what is the correct way of doing things with pure virtual classes?
>>
>> Thanks,

>
> You will have to derive Daffy from InterfaceDuck. Of course that way it
> is no longer really duck typing, but that should be a goal in itself
> anyway.


Corrrection: that should *not* be a goal in itself anyway.
 
Reply With Quote
 
 
 
 
Dombo
Guest
Posts: n/a
 
      07-24-2011
Op 24-Jul-11 20:23, TP schreef:
> 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/2...ifferent-from-
> 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.


That is not possible in C++. Casting won't work be cause is Daffy isn't
a InterfaceDuck. In this case casting would be lying to your compiler,
and the compiler will have its revenge on you sooner or later for doing
that.

> The fact that Daffy has a method Quack() as InterfaceDuck should be enough
> to allow that.


Nope, C++ doesn't work that way. You must explicitly derive from
InterfaceDuck so the compiler knows that it is your intention that Daffy
can be used wherever InterfaceDuck is expected (i.e there is a is-a
relationship between Daffy and InterfaceDuck). That two classes merely
have a method that happens to have the same name is not good enough for
C++.

> But perhaps there is no means to do that in C++?


Like I said before with templates may be able get a sort of duck typing
in C++.

However it will never be the same as in Python. The fundamental
difference between Python and C++ is that Python is a dynamically typed
language where as C++ is statically typed. This requires a different
approach and I doubt trying to write a program the Python way in C++ is
a good idea.


 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      07-24-2011
TP <(E-Mail Removed)> writes:

> 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:


You can't.

> 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/2...ifferent-from-
> the-old-variant-type-and-or-interfaces
>
> I would like to have a construction where the following affectation is
> possible:
>
> InterfaceDuck * d = new Daffy;


No way, unless InterfaceDuck and Daffy are linked by inheritance.

If you then call d->quack(), the compiler has to make sure *at compile
time* there actually is a method quack to call on d (even though it may
not be able to decide exactly which one will be called).

This is called static typing, a thing python doesn't do.

Typing is a basic programming notion, I suggest you read
http://en.wikipedia.org/wiki/Type_system

-- Alain.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      07-24-2011
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/2...ifferent-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(); }
};

 
Reply With Quote
 
TP
Guest
Posts: n/a
 
      07-24-2011
Nobody wrote:

> 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(); }
> };


Thanks all for your answers.

Template/proxy may be the better solution in my case (to avoid to be
compelled to do multiple inheritance in my real case).

But I have to go deeper in templates, which are still complicated to read
for me.

If I have the two solutions: (i) templating a class, or (ii) using multiple
inheritance, what is the best solution?

NOTE: if some (all?) of you understand Qt, this is my precise problem (Qt is
not the subject of this group, that's why I explain that only now): I define
a QWidget w1, which can include a QWidget w2 that must implement some
methods to be used (say: setText, readText). But if w2 is not given, w1
creates a default w2. w2 is a member of w1, in Python no type is needed for
w2, it must only provide setText and readText. But in C++, I must give a
type to w2 in the definition of w1. I could declare w1 as a template for w2.
But I could also make mandatory to have w2 to derive from an interface
(abstract class) containing setText and readText as pure virtual method; in
this case, as w2 will already derive from some Qt class, we have multiple
inheritance.
Do you understand my problem?

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      07-24-2011
On 24.07.11 18.11, Alain Ketterlin wrote:
> Come on, duck typing is a funny name for "no static typing, cross
> fingers at runtime". There is no such thing in C++, which is a
> statically typed, object-oriented language. This means that a call like
> o.quack() in C++ will never crash at runtime with a message like "no
> such method".


Well that is not true in all cases. E.g. the following code will call a
non-existent function and abort at runtime.

#include <stdio.h>

class A
{public:
virtual void foo() = 0;
A() { bar(); }
private:
void bar() { foo(); }
};

class B : public A
{public:
virtual void foo() { puts("B::foo"); }
};

int main()
{ B b;
return 0;
}


Marcel
 
Reply With Quote
 
Thomas Boell
Guest
Posts: n/a
 
      07-25-2011
On Sun, 24 Jul 2011 17:40:02 +0200
TP <(E-Mail Removed)> 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/2...ifferent-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++?


Well you could create something where you basically stuff your function
pointers into a std::map (similar in use to a hash), then look them up
by name and call them.

This way, the compiler will not be able to tell you if you make a typo.
The call will then fail (i. e. segfault) at runtime.

If that's what you want...


 
Reply With Quote
 
Qi
Guest
Posts: n/a
 
      07-25-2011
On 2011-7-24 23:40, TP wrote:
>
> Is it possible in C++?


As other mentioned, you have two methods to do it in C++,

1, Using template. Unlike the real duck typing, the type is
solved at compile time, not at runtime.
But using template maybe hard to implement OOP hierarchy.

2, Using interface. I think this is how duck typing should
be. If an object looks like a duck, walks like a duck,
even it's not a duck, it must have duck behavior, which
is IDuck.
This is easier to implement OOP hierarchy.


--
WQ
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      07-25-2011
On 24 Jul., TP wrote:
> NOTE: if some (all?) of you understand Qt, this is my precise problem (Qt is
> not the subject of this group, that's why I explain that only now): I define
> a QWidget w1, which can include a QWidget w2 that must implement some
> methods to be used (say: setText, readText). But if w2 is not given, w1
> creates a default w2. w2 is a member of w1, in Python no type is needed for
> w2, it must only provide setText and readText. But in C++, I must give a
> type to w2 in the definition of w1. I could declare w1 as a template for w2.
> But I could also make mandatory to have w2 to derive from an interface
> (abstract class) containing setText and readText as pure virtual method; in
> this case, as w2 will already derive from some Qt class, we have multiple
> inheritance.
> Do you understand my problem?


I think the template based approach should do:

template<class t_ClassTypeOfW2 = DefaultClassForW2>
class MyWidget : public QWidget
{
private:
t_ClassTypeOfW2 w2;

void foo ()
{
w2.SetText ("foo");
}
};

Now you can put any class into t_ClassTypeOfW2 as long as it has a
SetText method (no interfaces needed). However, if you would like to
determine the type of w2 at run-time (or change it), you'll have to
resort to the interface based solution.

Regards,
Stuart
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typing to Nullable (Of Date) vs typing to Date JimLad ASP .Net 0 01-26-2010 07:54 PM
typing location changes when I am typing. Ed Computer Support 5 11-11-2006 12:51 AM
Duct tape is -indeed- your friend Silverstrand The Lounge 4 09-16-2005 02:30 PM
Static Typing Where Possible and Dynamic Typing When Needed vladare Ruby 0 07-11-2005 11:54 AM
Finally, a solution to unsightly duct tape. Shel-hed Computer Support 5 05-27-2004 01:05 AM



Advertisments