469,282 Members | 2,359 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 26721
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<FirstClass,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 AntigenPresentingCellType : public CellType {};
class DendriticCellType : public AntigenPresentingCellType {};
class MacrophageCellType : public AntigenPresentingCellType {};
class BLymphocyteCellType : public AntigenPresentingCellType {};

class LymphocyteCellType : public CellType {};
class CD4CellType : public LymphocyteCellType {};
class CD8CellType : public LymphocyteCellType {};

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 LymphocyteCellType, 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 AntigenPresentingCellType : public CellType {};
class DendriticCellType : public AntigenPresentingCellType {};
class MacrophageCellType : public AntigenPresentingCellType {};
class BLymphocyteCellType : public AntigenPresentingCellType {};

class LymphocyteCellType : public CellType {};
class CD4CellType : public LymphocyteCellType {};
class CD8CellType : public LymphocyteCellType {};

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 LymphocyteCellType, 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 AntigenPresentingCellType : public CellType {};
class DendriticCellType : public AntigenPresentingCellType {};
class MacrophageCellType : public AntigenPresentingCellType {};
class BLymphocyteCellType : public AntigenPresentingCellType {};

class LymphocyteCellType : public CellType {};
class CD4CellType : public LymphocyteCellType {};
class CD8CellType : public LymphocyteCellType {};

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 LymphocyteCellType, 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 AntigenPresentingCellType : public CellType {};
class DendriticCellType : public AntigenPresentingCellType {};
[...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<CD4CellType*>(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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephen | last post: by
122 posts views Thread by Einar | last post: by
11 posts views Thread by Russ Green | last post: by
6 posts views Thread by Yahoo | last post: by
3 posts views Thread by Kiran B. | last post: by
7 posts views Thread by Prabhudhas Peter | last post: by
50 posts views Thread by titan nyquist | last post: by
5 posts views Thread by S S | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.