Inheritance & polymorphism

P

placid

Hi all,

I was just wondering, in Java there is an operator called instanceOf,
whats the equivalent in c++?

Thanks in advance to all.
 
D

David Hilsee

placid said:
Hi all,

I was just wondering, in Java there is an operator called instanceOf,
whats the equivalent in c++?

Thanks in advance to all.

The closest equivalent is dynamic_cast (the pointer variant), but it
resembles C#'s "as" more than it does instanceof.
 
P

placid

David said:
The closest equivalent is dynamic_cast (the pointer variant), but it
resembles C#'s "as" more than it does instanceof.

is there another way then a dynamic_cast ?
 
W

Werner

placid said:
is there another way then a dynamic_cast ?

Hi,

Unlike Java, C++ does not have a common polymorphic ancestor. This does
not stop you from doing the following, what may be more or less what
java does behind the scenes, I guess:

#include <typeinfo>
#include <iostream>

struct Base
{
bool instanceOf( Base const& other ) const
{
//Just doing this for clarity...
const std::type_info& myInfo( typeid(*this) );
const std::type_info& otherInfo( typeid(other) );

//Could have used: return( typeid(*this)==typeid(other) );
return ( myInfo == otherInfo );
}
virtual ~Base(){}
};

struct Derived : Base
{
};


int _tmain(int argc, _TCHAR* argv[])
{
Base base;
Derived d1;
Derived d2;
std::cout << "D1, D2:" << d1.instanceOf( d2 ) << std::endl;
std::cout << "D1, Base: " << base.instanceOf( d1 ) << std::endl;
std::cout << "Base, D1: " << d1.instanceOf( base ) << std::endl;

std::cin.get();
}

//Output:
D1, D2:1
D1, Base: 0
Base, D1: 0

You go figure!

Regards,

Werner
 
M

mlimber

placid said:
Hi all,

I was just wondering, in Java there is an operator called instanceOf,
whats the equivalent in c++?

Thanks in advance to all.

The typeid operator is similar but not equivalent. There is also the
Boost TypeTraits library
(http://boost.org/doc/html/boost_typetraits.html), which is also part
of the TR1 tentative extension to the C++ standard library and which
gives a compile-time value for type queries such as "Is class A a base
class of class B?"

Cheers! --M
 
J

Josh Mcfarlane

placid said:
is there another way then a dynamic_cast ?

Not built in, although you could code a static function to return the
class type. The question you have to ask yourself is why do you need it?
 
D

David Hilsee

I'm not familier with java, so can you explain what does instanceOf do?

It allows you to determine if an object is an instance of a class or one of
the class's children. It's usually used to determine if a you can cast an
object to a certain type. Its usage will often look like this:

Something x = ...;
if ( x instanceof SomethingElse ) { // is x a SomethingElse?
SomethingElse x2 = (SomethingElse)x; // cast it
// do something with x2
}
 
R

red floyd

David said:
It allows you to determine if an object is an instance of a class or one of
the class's children. It's usually used to determine if a you can cast an
object to a certain type. Its usage will often look like this:

Something x = ...;
if ( x instanceof SomethingElse ) { // is x a SomethingElse?
SomethingElse x2 = (SomethingElse)x; // cast it
// do something with x2
}


if (SomethingElse x2 = dynamic_cast<SomethingElse*>(&x))
{
// do something with x2
}
else
{
// x is not a SomethingElse
}
 
D

David Hilsee

red floyd said:
if (SomethingElse x2 = dynamic_cast<SomethingElse*>(&x))
{
// do something with x2
}
else
{
// x is not a SomethingElse
}

Yes, ignoring a missing *, that's roughly the C++ equivalent of the Java
code I provided.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top