Re: Pointer to multiple parent classes
On 25 , 01:12, dataangel <k04j...@gmail.com> wrote:
I have two classes, foo and bar. I would like to write a function that
takes a pointer to any object that is of a class that subclasses both
foo AND bar. So if I had:
class apple : public foo, public bar {};
class orange : public foo, public bar {};
class tangerine : public foo {};
class mango : public bar {};
Then I would want my function to be able to take apple pointers and
orange pointers, but not tangerine pointers or mango pointers.
Does C++ have a clean way of doing this? Or do I have to implement a
dummy class that inherits foo and bar, and have apple and orange
inherit from the dummy type?
The best solution I've thought of so far is some template hackery and
isn't a whole lot prettier. An Extends<T1, T2 ... TN> template that
automates the generating of the dummy class, and if you use it
everywhere ensures you never have redundant dummy types. The only
issue with it is that it would probably need to use the boost
preprocessor library (since C++ doesn't support variable template
arguments). Whenever you want to use multiple inheritance, you'd
inherit from Extends instead:
class apple : public Extends<foo, bar> {};
class orange : public Extends<foo, bar> {};
And Extends would look like this (once generated):
template<class T1, class T2>
class Extends : public T1, public T2 {};
Then you write functions that take Extends<foo, bar>. But there's a
problem with this too -- there's an explosion of types that need to be
generated. Presumably, if you had:
class pomegranate : public Extends<foo, bar, blarg> {};
You would still want to be able to pass pomegranate pointers to
functions that took Extends<foo, bar> pointers., or Extends<foo,
blarg> pointers, or Extends<bar, blarg> pointers. So you would have to
do something like this instead:
class pomegranate : public Extends<foo, Extends<bar, blarg> > {};
But that what if someone writes a function that takes a different
order like Extends<bar, Extends<foo, blarg>>? So this solution has
problems too. Ideas?
If you don't need to overload you function on the condition "if
object inherits foo and bar then do this, otherwise do something
else", then just do as another poster suggested.
template <class T>
void baz(T * p)
{
foo * f = p;
bar * b = p;
// do your stuff
}
But what if you want to overload this function with something like
this?
// if object only inherits foo, then this function should be calleed
void baz(foo *);
Here boost::enable_if comes to help.
#include <iostream>
#include <typeinfo>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/mpl/and.hpp>
using namespace boost::mpl;
using namespace boost;
using namespace std;
struct foo {};
struct bar {};
template <class T>
struct inherits_foo_bar :
and_<is_base_and_derived<foo, T>,
is_base_and_derived<bar, T> >
{
};
// this function only accepts objects, which
// inherit foo and bar
template <class T>
typename enable_if<inherits_foo_bar<T>, void>::type
baz(T * p)
{
cout << typeid(*p).name() << " inherits foo & bar!" << endl;
}
void baz(foo * p)
{
cout << typeid(*p).name() << " inherits foo!" << endl;
}
void baz(bar * p)
{
cout << typeid(*p).name() << " inherits bar!" << endl;
}
class apple : public foo, public bar {};
class orange : public foo, public bar {};
class tangerine : public foo {};
class mango : public bar {};
int main()
{
apple a;
orange o;
tangerine t;
mango m;
baz(&a);
baz(&o);
baz(&t);
baz(&m);
}
There is one noticable drawback with this solution: baz
has to be function template. But that does not mean
that you should put all the function code to header
file. You can you the following trick.
template <class T>
typename enable_if<inherits_foo_bar<T>, void>::type
baz(T * p)
{
baz_impl(p, p);
}
// do your stuff here
void baz_impl(foo *, bar *);
Roman Perepelitsa.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]