473,698 Members | 2,102 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 27168
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
2077
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 the SQL Data Compare 3.0 application 14 day trial and it seemed really good however does a bit more...
122
5289
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
9717
by: Russ Green | last post by:
How does this: public TimeSpan Timeout { get { return timeout; } set { timeout = value; if(timeout < licenseTimeout) licenseTimeout = timeout; }
6
11333
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
8899
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
12888
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 or simple code snippet.. Thank U -- Peter...
5
37841
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
20308
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
3969
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
9152
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8855
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7708
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4358
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3037
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
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.