Stuart said:
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
Thanks for your answer. At first sight templating seems to me easier than
multiple inheritance (I have read about multiple inheritance this evening,
there is some subtleties, sometimes).
Below is my solution, in the case of the "duck example" of this post,
inspired from your answer and the one of Nobody.
I have to read further concerning what a proxy is, because below I use this
denomination without really mastering it!
I fact, I realize only now that what I needed is *static* duck typing. So
templating (parametric polymorphism) is the easier solution, it seems.
1/ The version with interface:
==============================
#include <iostream>
class InterfaceDuck
{
public:
virtual void Quack() = 0;
};
class Daffy
{
public:
void Quack()
{
std::cout << "coin coin, I'm Daffy" << std::endl;
}
};
class Donald
{
public:
void Quack()
{
std::cout << "coin coin, I'm Donald" << std::endl;
}
};
template <class T>
class ProxyDuck : public InterfaceDuck
{
public:
T * ref;
ProxyDuck(T * ref) : ref(ref) {}
void Quack() { ref->Quack(); }
};
int main( void )
{
Daffy * dada = new Daffy;
Donald * dodo = new Donald;
ProxyDuck<Daffy> * d = new ProxyDuck<Daffy> ( dada );
ProxyDuck<Donald> * e = new ProxyDuck<Donald> ( dodo );
std::cout << "Daffy is going to quack" << std::endl;
d->Quack();
std::cout << "Donald is going to quack" << std::endl;
e->Quack();
delete d;
delete e;
return 0;
}
2/ The version without interface:
=================================
#include <iostream>
class Daffy
{
public:
void Quack()
{
std::cout << "coin coin, I'm Daffy" << std::endl;
}
};
class Donald
{
public:
void Quack()
{
std::cout << "coin coin, I'm Donald" << std::endl;
}
};
template <class T>
class ProxyDuck
{
public:
T * ref;
ProxyDuck(T * ref) : ref(ref) {}
void Quack() { ref->Quack(); }
};
int main( void )
{
Daffy * dada = new Daffy;
Donald * dodo = new Donald;
ProxyDuck<Daffy> * d = new ProxyDuck<Daffy> ( dada );
ProxyDuck<Donald> * e = new ProxyDuck<Donald> ( dodo );
std::cout << "Daffy is going to quack" << std::endl;
d->Quack();
std::cout << "Donald is going to quack" << std::endl;
e->Quack();
delete d;
delete e;
return 0;
}