Connecting Tech Pros Worldwide Help | Site Map

How to get the name of a parent class statically?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:51 AM
Ronald Landheer-Cieslak
Guest
 
Posts: n/a
Default 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

  #2  
Old July 23rd, 2005, 04:52 AM
Ronald Landheer-Cieslak
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.