473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2506
"placid" <Bu****@gmail.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.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******** *************@o 13g2000cwo.goog legroups.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******** *************@o 13g2000cwo.goog legroups.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<So methingElse*>(& x))
{
// do something with x2
}
else
{
// x is not a SomethingElse
}
Sep 20 '05 #10

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

Similar topics

37
2825
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 use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
12
7043
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 I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
3
41531
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 am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
10
3027
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
1773
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; namespace ConsoleProject1 { public class Base
13
3242
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 happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
6
3812
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 me somewhat as I have always thought you get code reuse "for free" with inheritance. Am I missing something?. Will someone care to explain ??
8
20598
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 fundanmental concept so i can remember it forever as it is a common interview question.... Thanks in advance
1
1603
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 { public class Program
17
3868
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8845
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
8743
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...
0
7355
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...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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.