Archived

This topic is now archived and is closed to further replies.

aboeing

who was a class inherited from? (C++)

Recommended Posts

Hi, Im trying to figure out what a certain class was inherited from. The problem is that one class inherits another and I want it to execute specific code depending on the class it was inherited from. I cant give all classes the same function which is implemented differently because I use multiple inheritance with these classes somewhere else.
class A {};
class B {};
template <typename AB>
class X : public AB {
void DoSomethingDependingOnAorB{};
};
//somewhere else:

class Z : public A, public B {
};
Is there some way to solve this? const type_info& t1 = typeid(this); printf("%s\n",t1.name()); will print out the correct name, but if I try a dynamic cast it gives me this: error C2683: dynamic_cast : ''MaybeRC'' is not a polymorphic type and if I force virtual inheritance, it claims to be always able to cast to the class which I desire. Is there some other way to figure out what class it was inherited from? (searching through the name string is not viable, because different compilers name the strings differently.)

Share this post


Link to post
Share on other sites
I gotta admit I''m having a hard time figuring out what you''re trying to do. It would probably help if you could post more code so the errors had some context. I''m not a particularly big fan of multiple inheritance as it is, but I''d give your problem a shot if I could understand it better.

Share this post


Link to post
Share on other sites
First off my initial response is that any design that would need such a capability could use some fundamental changes.

But, good design aside:

template<typename T>
void DoDifferent(T var)
{
A* tryA = dynamic_cast<A*>(&var);
if(tryA != 0)
//its of type A

B* tryB = dynamic_cast<B*>(&var);
if(tryB != 0)
//its of type B

}

To use such c++ capabilities most compliers require you to manualy turn on ''run time type identification'' or rtti.
But, in 90% of cases, if your using rtti there is a better, simpler way to do it instead.

Share this post


Link to post
Share on other sites
Sounds like you just need to make the function virtual in the base classes. The implementation from whichever class you derive from will be the one that runs.

[edited by - merlin9x9 on October 22, 2003 2:02:18 AM]

Share this post


Link to post
Share on other sites
You dont need to do any runtime stuff for this, the information concerning who you are inheriting from are available at compiletime!

I would go with the suggestion given by the AP, use template specialization.

Share this post


Link to post
Share on other sites

class A {};
class B {};

template<typename AorB>
class X : public AorB
{
};

template<>
class X<A> : public A
{
// specialize for A

};

template<>
class X<B> : public B
{
// specialize for B

};

class Z : public A, public B
{
// shizzle, inherit from both

};

Share this post


Link to post
Share on other sites

#include <iostream>

class A {};
class B {};

template <class T>
class C : public T
{
template <class U> struct DoSomething_
{
// ASSERT maybe?

void operator () (void) {}
};
template <> struct DoSomething_<::A>
{
void operator () (void)
{
std::cout << "Do something A" << std::endl;
}
};
template <> struct DoSomething_<::B>
{
void operator () (void)
{
std::cout << "Do something B" << std::endl;
}
};
public:
C () {}
DoSomething_<T> DoSomething;
};


int main()
{
C<A> test1;
C<B> test2;
test1.DoSomething();
test2.DoSomething();
return 0;
}


DoSomething inst a member function but rather an instance of a functor. Kinda interesting when you think about it

daerid method is also good.

[edited by - Amnesty on October 22, 2003 3:11:40 AM]

Share this post


Link to post
Share on other sites
Thanks guys, partial template specialization sounds like exactly what I want. (and thanks to dareid for spelling it out )

First off my initial response is that any design that would need such a capability could use some fundamental changes.
These were my thoughts also, but im not sure how to redesign it.

I basically want to use this to build up customized base classes, ie: reference counted? simplememorymanaged? heapdetection? memorypooled? statuslogging? factoryobject? etc..
the specific problem i was trying to solve was that if heapdetection was used, then the simple memory manager should not automatically delete the object when garbagecollection is called, but otherwise i guess just asume everything was in the freestore and delete it. likewise for things with reference counting..

is there a better design?

Share this post


Link to post
Share on other sites
Hi guys, just another question now on template specialization,
this code snippet illustrates the problem:

class A {};

template <typename T>
class A {};
class B {};

template<typename AorB>
class X : public AorB
{};

template<> class X<A> : public A
{
// specialize for A

};

//but NOW what:

template<typename XorY>
class Z : public XorY
{};

//the following code is wrong:

template<> class X<A> : public A
{
//specialize for A

//!but I also want this to specialize for X<A>

};


Basically, how do I specialize a template for a base class, when the template inherits from another templated class (and that class contains the base class).

Thanks!

Share this post


Link to post
Share on other sites
Two days later,..
The answer to my question, for future reference is:
-Download loki
-Use the SUPERSUBCLASS macro in

How to specialize for certain functions?
-Use the Int2Type template in

Example:

int A() {
return SpecificA(Int2Type<SUPERSUBCLASS(BaseClass,CurrentClass)>());
}

Im thinking I should just spend some time sitting down and learning loki inside out.

Edit: damn " < "'s

[edited by - aboeing on November 1, 2003 4:24:46 AM]

Share this post


Link to post
Share on other sites