How to compare object types in C++?

B

Bryan

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
 
G

Gianni Mariani

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.
 
V

Victor Bazarov

Bryan said:
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
 
B

Bryan

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?
 
B

Bryan

Bryan said:
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
{
....
}
 
J

John Harrison

Bryan said:
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
 
T

Thomas J. Gritzan

Bryan said:
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.
 
D

Dave Rahardja

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top