473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

cast classes to common ancestor

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

Oct 7 '07 #1
2 2884
de**********@gmail.com 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.
Oct 7 '07 #2
Alf P. Steinbach wrote:
* de**********@gmail.com:
>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
Oct 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ulrich Heinen | last post by:
Let's say I have two classes A and B with similar goals but completely different implementations. In my application code I want to use class A exclusively, but instances of B should also be...
30
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking...
14
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
1
by: ankit | last post by:
Hello, Please put some light on, What are new style classes and classic style classes in python. The basic differences in them. And How can I decide to choose one.
16
by: the.duckman | last post by:
G'Day. Anybodey got an idea on this problem. Say I have a function object doCast(object obj, Type t); It's job is to cast the obect (obj) to a new type (t) and return it. Sounds so...
4
by: cmb99 | last post by:
I have a Symbol class, "MSFT". It has a bid and offer. "Dell" is also an instance of such a class. Sometimes, though, I want to combine two Symbol's into one, e.g. "MSFT" * "DELL", so that, I can...
3
by: Tim H | last post by:
I'm wondering if there is a way to do this properly. I feel like I am missing something. I have two distinct classes. They don't have much at all in common. I want a vector-like container that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.