473,378 Members | 1,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Inheritance & polymorphism

Hi all,

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

Thanks in advance to all.

Sep 19 '05 #1
10 2481
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.

--
David Hilsee
Sep 19 '05 #2

placid wrote:
Hi all,

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

Introduction to C++.

Sep 19 '05 #3

David Hilsee wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.


is there another way then a dynamic_cast ?

--
David Hilsee


Sep 19 '05 #4

placid wrote:
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

Sep 19 '05 #5
placid wrote:
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

Sep 19 '05 #6
placid wrote:
David Hilsee wrote:
"placid" <Bu****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.


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?

Sep 19 '05 #7
I'm not familier with java, so can you explain what does instanceOf do?

Sep 19 '05 #8
<gu*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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
}

--
David Hilsee
Sep 20 '05 #9
David Hilsee wrote:
<gu*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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
}

if (SomethingElse x2 = dynamic_cast<SomethingElse*>(&x))
{
// do something with x2
}
else
{
// x is not a SomethingElse
}
Sep 20 '05 #10
"red floyd" <no*****@here.dude> wrote in message
news:Jr*****************@newssvr27.news.prodigy.ne t...
David Hilsee wrote:
<gu*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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
}

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.

--
David Hilsee
Sep 20 '05 #11

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

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
10
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
1
by: relient | last post by:
Hi, I just started the chapter on "Inheritance" and I'm a bit confused so I have three questions: first, here's the code: using System; using System.Collections.Generic; using System.Text; ...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
8
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the...
1
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { ...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.