How to get the name of a parent class statically?

  • Thread starter Ronald Landheer-Cieslak
  • Start date
R

Ronald Landheer-Cieslak

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
 
R

Ronald Landheer-Cieslak

Victor said:
Ronald said:
[...]
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?
Just imagine, what if your class has more than one base class? What would
you expect in that case as "the most remote ancestor"?
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..
Maybe in Visual Basic, where there is no multiple inheritance...
I only write C and C++ - by Basic days are far behind me and I'd like to
keep it that way...

Thanks,

rlc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,064
Members
47,672
Latest member
svaraho

Latest Threads

Top