Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get the name of a parent class statically?

Ronald Landheer-Cieslak
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello all,

We're coding a really nice implementation of a generic abstract factory
at the moment, but we've come to a slight impass we have to work around:
when automagically registering a class to be built by the builder, we
are using a class and a macro that look like this:

template<
typename ClassT,
typename ParentT = typename ClassT::ParentType,
typename BuilderT = clsBuilder<typename ParentT> >
struct Initializer
{
Initializer(typename BuilderT::tKeyType oKey)
{
BuilderT::RegisterClass(oKey, ClassT::Create);
}

Initializer(typename BuilderT::tKeyType oKey,
typename BuilderT::tCreatorFunctor functor)
{
BuilderT::RegisterClass(oKey, functor);
}
};

#define REGISTER_CLASS( C, K ) \
static Initializer<C> C##__Initializer(K)

This requires a typedef (ParentType) in the class, its ancestor or (in
an alternative approach we tried) in the builder.

We were looking for a way to find the most remote - or even a direct -
ancestor of a given class C statically, but we didn't find one. However,
we're pretty sure the compiler has the information somewhere, as the
class was declared as having one or more parents. Is there any
*standard* way to gleen this information from the compiler?

Thanks,

rlc

Ronald Landheer-Cieslak
Guest
 
Posts: n/a
#2: Jul 23 '05

re: How to get the name of a parent class statically?


Victor Bazarov wrote:[color=blue]
> Ronald Landheer-Cieslak wrote:[color=green]
>> [...]
>> We were looking for a way to find the most remote - or even a direct -
>> ancestor of a given class C statically, but we didn't find one. However,
>> we're pretty sure the compiler has the information somewhere, as the
>> class was declared as having one or more parents. Is there any
>> *standard* way to gleen this information from the compiler?[/color]
> Just imagine, what if your class has more than one base class? What would
> you expect in that case as "the most remote ancestor"?[/color]
A direct ancestor would suffice (a little recursive template magic will
get me the most remote ancestor if I want to). As for multiple
inheritance, I'd say there are plenty of ways to deal with that if we
want: a sort of get_parent(class, parent_id) where parent_id is a
non-negative integer would do the trick..

I'm not surprised the answer is "no", though: it just means the parent
class needs some kind of support for my factory or where-ever I use my
macro to register the class in the factory I'll have to specify which
factory I want to register in - which is not necessarilly a bad thing..
[color=blue]
> Maybe in Visual Basic, where there is no multiple inheritance...[/color]
I only write C and C++ - by Basic days are far behind me and I'd like to
keep it that way...

Thanks,

rlc
Closed Thread