468,291 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

How to get the name of a parent class statically?

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
Jul 23 '05 #1
1 1340
Victor Bazarov wrote:
Ronald Landheer-Cieslak wrote:
[...]
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
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rick | last post: by
7 posts views Thread by ~toki | last post: by
14 posts views Thread by Altman | last post: by
1 post views Thread by mark.martinez2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.