Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cast classes to common ancestor

Reply
Thread Tools

cast classes to common ancestor

 
 
deadguysfrom@gmail.com
Guest
Posts: n/a
 
      10-07-2007
Ive got two classes in the same class hierarchy, and I want to find
their common ancestor. Any idea how to do this?

class A {};
class B : public A {};
class C : public B {};
class D : public B {};

I have pointers to C and D, I want pointers to B.

thanks

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Ive got two classes in the same class hierarchy, and I want to find
> their common ancestor. Any idea how to do this?
>
> class A {};
> class B : public A {};
> class C : public B {};
> class D : public B {};
>
> I have pointers to C and D, I want pointers to B.
>

int main()
{
D* d = new D;

B* b = d;
}

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-07-2007
* (E-Mail Removed):
> Ive got two classes in the same class hierarchy, and I want to find
> their common ancestor. Any idea how to do this?
>
> class A {};
> class B : public A {};
> class C : public B {};
> class D : public B {};
>
> I have pointers to C and D, I want pointers to B.


See the reply by Ian Collins else-thread.

If your enquiry is of a more academic, theoretical nature:

You can check whether the classes /have/ a common ancestor by using
static_cast, possibly combined with SFINAE.

You can go the opposite way, finding a pointer to the complete object
(of the most derived class), by using dynamic_cast<void*>, provided the
pointer you have is a pointer to an object of polymorphic class. This
is sometimes useful, e.g. for hashing. But it doesn't tell you what the
most derived class is, except you can get a run-time description.

You can not enumerate the class hierarchy by means of only language and
standard library functionality.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-07-2007
Alf P. Steinbach wrote:

> * (E-Mail Removed):
>> Ive got two classes in the same class hierarchy, and I want to find
>> their common ancestor. Any idea how to do this?
>>
>> class A {};
>> class B : public A {};
>> class C : public B {};
>> class D : public B {};
>>
>> I have pointers to C and D, I want pointers to B.

>
> See the reply by Ian Collins else-thread.
>
> If your enquiry is of a more academic, theoretical nature:
>
> You can check whether the classes /have/ a common ancestor by using
> static_cast, possibly combined with SFINAE.


Since it sounded like a nice template exercise, I just tried to do that,
i.e., I tried to whip up a template

template < typename A, typename B >
class have_common_base {

// some SFINAE magic

public:

static bool const value =
// some more magic

};

so that have_common_base<A,B>::value would be true if and only if A and B
are derived from some common base class.

I failed, and your hint didn't quite cut it for me

How would you do it?

[snip]


Best

Kai-Uwe Bux
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-07-2007
* Kai-Uwe Bux:
> Alf P. Steinbach wrote:
>
>> * (E-Mail Removed):
>>> Ive got two classes in the same class hierarchy, and I want to find
>>> their common ancestor. Any idea how to do this?
>>>
>>> class A {};
>>> class B : public A {};
>>> class C : public B {};
>>> class D : public B {};
>>>
>>> I have pointers to C and D, I want pointers to B.

>> See the reply by Ian Collins else-thread.
>>
>> If your enquiry is of a more academic, theoretical nature:
>>
>> You can check whether the classes /have/ a common ancestor by using
>> static_cast, possibly combined with SFINAE.

>
> Since it sounded like a nice template exercise, I just tried to do that,
> i.e., I tried to whip up a template
>
> template < typename A, typename B >
> class have_common_base {
>
> // some SFINAE magic
>
> public:
>
> static bool const value =
> // some more magic
>
> };
>
> so that have_common_base<A,B>::value would be true if and only if A and B
> are derived from some common base class.
>
> I failed, and your hint didn't quite cut it for me
>
> How would you do it?


Uh, that was stupid of me. Doing what you tried (and what I literally
wrote) requires first finding the common ancestor class... Catch 22.

Fix:

"You can check whether the classes /have/ a given common ancestor by
using static_cast, possibly combined with SFINAE."

Like e.g.

<code>
#include <iostream>
#include <ostream>

typedef char Nope;
struct Yep{ char x[2]; };

template< typename T >
Nope matches( ... );

template< typename T >
Yep matches( T* );

enum { yepSize = sizeof( Yep ), nopeSize = sizeof( Nope ) };

template< typename Derived, typename Base >
bool hasBase(){ return sizeof( matches<Base>( static_cast<Derived*>(0) )
) == yepSize; }

template< typename A, typename B, typename Base >
bool haveCommonBase() { return hasBase<A, Base>() && hasBase<B, Base>(); };

struct Base {};
struct A: Base {};
struct B: Base {};

struct X {};
struct Y {};

int main()
{
using namespace std;
cout << haveCommonBase<A, B, Base>() << endl;
cout << haveCommonBase<X, Y, Base>() << endl;
}
</code>

Here 'hasBase' can yield false positives, for Base = void. Instead one
may use the more elaborate Boost checks.

Cheers,

- Alf (impreftc, and rowing energetically )

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
java.lang.NoSuchMethodError: wm.common.session.Common.getCustRptListFromMax Denny Java 1 05-01-2008 07:33 AM
Why doesn't ri look into ancestor classes to find help? Ilmari Heikkinen Ruby 0 01-17-2005 12:29 AM
How to run _init for all ancestor classes in diamond inheritance Amir Karger Perl Misc 9 12-10-2003 03:59 PM
JTree cut single and multiple child and ancestor nodes not function the right way! Johnny Java 0 10-02-2003 06:44 PM



Advertisments