473,545 Members | 305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare object types in C++?

If I have two classes derived from the same base class, how can I check
if they are the same type?

class Base : CObject
{
public:
Base() {};
~Base() {};
}

class FirstClass : public Base {};
class SecondClass : public Base {};

I would like to do something like:
if (FirstClass == SecondClass)
{
// Whatever
}

Maybe this is an MFC thing? Any suggestions?

I do not want to enable RTTI if possible, which is why I am using CObject.

Thanks,
B
Jun 23 '07 #1
7 27161
Bryan wrote:
...
Maybe this is an MFC thing? Any suggestions?
there are various meta programming templates that do what you suggest.
>
I do not want to enable RTTI if possible, which is why I am using CObject.
I'm not sure RTTI is the right answer anyway, however, RTTI is not a bit
deal. I'd enable it if I were you.

Here is a classic - is-same template.
question for you - should a const FirstClass be the same as a volatile
FirstClass ? If so, you
struct FalseType { enum { value = false }; };
struct TrueType { enum { value = true }; };
template <typename T1, typename T2>
struct IsSame
{
typedef FalseType Result;
};
template <typename T>
struct IsSame<T,T>
{
typedef TrueType Result;
};

Then you can replace "if (FirstClass == SecondClass)" with

if ( IsSame<FirstCla ss,SecondClass> ::Result::value )

Which in this case will allways be false.
Jun 23 '07 #2
Bryan wrote:
If I have two classes derived from the same base class, how can I
check if they are the same type?

class Base : CObject
{
public:
Base() {};
~Base() {};
}

class FirstClass : public Base {};
class SecondClass : public Base {};

I would like to do something like:
if (FirstClass == SecondClass)
{
// Whatever
}
Types are not objects in C++. And only objects can be compared.
There is such thing as std::type_info, objects of which you can
compare. But in your particular case, 'FirstClass' and 'SecondClass'
_are_ different, according to the rules of the language. What do
you hope to accomplish by comparing them? What does it mean to you
that two types are equal? Same size? Same alignment requirements?
Same internal layout? And two files do have all those things the
same, what's it to you that they are "the same"? What are you going
to do with that information?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '07 #3
Several people asked why I was doing this so Ill try to explain:

I have the following Cell class:
class Cell
{
....
CellType m_type;
}

Where CellType is like this:
class AntigenPresenti ngCellType : public CellType {};
class DendriticCellTy pe : public AntigenPresenti ngCellType {};
class MacrophageCellT ype : public AntigenPresenti ngCellType {};
class BLymphocyteCell Type : public AntigenPresenti ngCellType {};

class LymphocyteCellT ype : public CellType {};
class CD4CellType : public LymphocyteCellT ype {};
class CD8CellType : public LymphocyteCellT ype {};

class GenericCellType : public CellType {};

Now I can use the type objects to determine easily if a Cell object is
of type CD4 like so:
m_type.IsKindOf (RUNTIME_CLASS( CD4CellType))

or if this same object is a type of LymphocyteCellT ype, etc.

I liked this because I can use the inheritance of this type with
IsKindOf() to see what class or subclass my objects are in. Way better
than enums.

So now I want to see if I have two cells if cell1.m_type ==
cell2.m_type. I was thinking that I just need to know if cell1.m_type
is an object of the same class as cell2.type.

But maybe my thinking is wrong here and I should not be doing this?

Thoughts?
Jun 24 '07 #4
Bryan wrote:
Several people asked why I was doing this so Ill try to explain:

I have the following Cell class:
class Cell
{
...
CellType m_type;
}

Where CellType is like this:
class AntigenPresenti ngCellType : public CellType {};
class DendriticCellTy pe : public AntigenPresenti ngCellType {};
class MacrophageCellT ype : public AntigenPresenti ngCellType {};
class BLymphocyteCell Type : public AntigenPresenti ngCellType {};

class LymphocyteCellT ype : public CellType {};
class CD4CellType : public LymphocyteCellT ype {};
class CD8CellType : public LymphocyteCellT ype {};

class GenericCellType : public CellType {};

Now I can use the type objects to determine easily if a Cell object is
of type CD4 like so:
m_type.IsKindOf (RUNTIME_CLASS( CD4CellType))

or if this same object is a type of LymphocyteCellT ype, etc.

I liked this because I can use the inheritance of this type with
IsKindOf() to see what class or subclass my objects are in. Way better
than enums.

So now I want to see if I have two cells if cell1.m_type ==
cell2.m_type. I was thinking that I just need to know if cell1.m_type
is an object of the same class as cell2.type.

But maybe my thinking is wrong here and I should not be doing this?

Thoughts?
Sorry typed this in wrong,

CellType is defined like this:
class CellType : public CObject
{
....
}
Jun 24 '07 #5
Bryan wrote:
Bryan wrote:
>Several people asked why I was doing this so Ill try to explain:

I have the following Cell class:
class Cell
{
...
CellType m_type;
}

Where CellType is like this:
class AntigenPresenti ngCellType : public CellType {};
class DendriticCellTy pe : public AntigenPresenti ngCellType {};
class MacrophageCellT ype : public AntigenPresenti ngCellType {};
class BLymphocyteCell Type : public AntigenPresenti ngCellType {};

class LymphocyteCellT ype : public CellType {};
class CD4CellType : public LymphocyteCellT ype {};
class CD8CellType : public LymphocyteCellT ype {};

class GenericCellType : public CellType {};

Now I can use the type objects to determine easily if a Cell object is
of type CD4 like so:
m_type.IsKindO f(RUNTIME_CLASS (CD4CellType))

or if this same object is a type of LymphocyteCellT ype, etc.

I liked this because I can use the inheritance of this type with
IsKindOf() to see what class or subclass my objects are in. Way
better than enums.

So now I want to see if I have two cells if cell1.m_type ==
cell2.m_type . I was thinking that I just need to know if cell1.m_type
is an object of the same class as cell2.type.

But maybe my thinking is wrong here and I should not be doing this?

Thoughts?

Sorry typed this in wrong,

CellType is defined like this:
class CellType : public CObject
{
...
}
I agree with Gianni, you should turn on RTTI and forget all the MFC
rubbish. RTTI really is very lightweight. No idea why it's off by
default in earlier MS compilers. They turned it on by default in the
latest compiler.

If you turn on RTTI you can do it this way

if (typeid(cell1.m _type) == typeid(cell2.m_ type))
...

I'm sure there's a way in MFC, but you'll have to ask in an MFC group
about that. This is course is the big disadvantage of choosing
non-standard methods when the equivalent standard methods exist. You're
excluding yourself from the rest of the C++ community.

Whether type comparisons are a good idea in your case is hard to say.
Not enough information.

john
Jun 24 '07 #6
Bryan wrote:
Several people asked why I was doing this so Ill try to explain:

I have the following Cell class:
class Cell
{
...
CellType m_type;
}
m_type here is __always__ of type CellType. Polymorphism only works on
pointers and references. Let's assume it's a pointer:

CellType* m_type;
Where CellType is like this:
class AntigenPresenti ngCellType : public CellType {};
class DendriticCellTy pe : public AntigenPresenti ngCellType {};
[...more cell types...]
>
Now I can use the type objects to determine easily if a Cell object is
of type CD4 like so:
m_type.IsKindOf (RUNTIME_CLASS( CD4CellType))
You can do this with RTTI like so:

if (dynamic_cast<C D4CellType*>(m_ type)) != NULL)
{
// m_type points to a CD4CellType or derived type.
}
So now I want to see if I have two cells if cell1.m_type ==
cell2.m_type. I was thinking that I just need to know if cell1.m_type
is an object of the same class as cell2.type.
With RTTI:
if (typeid(*cell1. m_type) == typeid(*cell2.m _type))
{ /* same type */ }

If your type objects are singletons (there is only one object of each type
class and m_type only points to one of this objects), you could compare the
pointers to see if the cells have the same CellType.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Jun 24 '07 #7
On Sat, 23 Jun 2007 13:46:31 -0700, Bryan <br***@no.spam. comwrote:
>If I have two classes derived from the same base class, how can I check
if they are the same type?

class Base : CObject
{
public:
Base() {};
~Base() {};
}

class FirstClass : public Base {};
class SecondClass : public Base {};

I would like to do something like:
if (FirstClass == SecondClass)
{
// Whatever
}
In addition to the replies seen here, may I also add that this desire to
examine the derived types of a base class is indicative of a serious design
problem? Is there no way to add a virtual function to Base that will give you
access to the information you need without resorting to RTTI?

In my opinion, RTTI outside of serialization and low-level data manipulation
is always suspect.

-dr
Jun 27 '07 #8

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

Similar topics

3
2066
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index has cascade set on one db and not on another, or unique on one and not the other, Constraints, Triggers, Stored procs, Users, Roles. I downloaded...
122
5206
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) { /* array differs from value, do something*/
11
9690
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
6
11329
by: Yahoo | last post by:
I am trying to determine if an object inherits a given interface at runtime and cant figure out how to do it. if we knew the interface type at design time... if (x is typeof(InterfaceName)) but how do we do it when we dont know the interface until runtime. What a want to do... void myfx(Type type) if (x is typeof(type)) (ILLEGAL)
3
8887
by: Kiran B. | last post by:
Hi, I am new to .net. I have two Data Structure Type ... Sturcture A and Structure B. Structure A Public Fname as String Public LastName as String Public City as String Public Zip as String End Structure
7
12862
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function...
5
37764
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
50
20241
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
5
3960
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here. .... }; Here I am not passing compare function, but I want to do it into the
0
7464
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7396
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7751
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.