473,461 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

questions

1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";
-----

2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java's keyword 'instanceof'
I believe not, but hope will die last ;-)

Thanks
Jul 19 '05 #1
2 1677

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:bk**********@nic.grnet.gr...
1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";
The standard language doesn't support hashing, but the standard map class has the
semantics you want (it internally uses a tree representation).

#include <map>
#include <string>
using namespace std;

map<string, string> hash;

hash["my_name"] = "paul";

and so forth.
2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java's keyword 'instanceof'
I believe not, but hope will die last ;-)


Well, in your above example, you can't pass a B to GiveMeAClass (since you didn't make A publicly
inheritted). However if you fix that, and you make A polymorhphic (that is, make it have at least one
virtual function), you can:

void GiveMeAClass(A& a) {
if(dynamic_cast<A*>(&a))
cout << "Give me an A!\n";
else if(dynamic_cast<B*>(&b))
cout << "Give me a B!\n";
else
cout << "What is this?\n";
}
}

Alternatively you could do a similar thing with typeinfo:

if(typeid(a) == typeid (A))
There are small subtle differences between the two but you get the idea.
Jul 19 '05 #2
> > 1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:
-----
hash["my_name"] = "paul";
hash["my_mail"] = "sp**@here.now";


The standard language doesn't support hashing, but the standard map class has the
semantics you want (it internally uses a tree representation).

#include <map>
#include <string>
using namespace std;

map<string, string> hash;

hash["my_name"] = "paul";

and so forth.
2. we have this code:
-----------------
class A { ... };
class B : A { ... };
void GiveMeAClass(A &a) { ... }
-----------------
In function GiveMeAClass we can pass an A or B object too.
Is there a method inside GiveMeAClass to determine in run-time if passed object is from A or B?
Something like Java's keyword 'instanceof'
I believe not, but hope will die last ;-)


Well, in your above example, you can't pass a B to GiveMeAClass (since you didn't make A publicly
inheritted). However if you fix that, and you make A polymorhphic (that is, make it have at least one
virtual function), you can:

void GiveMeAClass(A& a) {
if(dynamic_cast<A*>(&a))
cout << "Give me an A!\n";
else if(dynamic_cast<B*>(&b))
cout << "Give me a B!\n";
else
cout << "What is this?\n";
}
}

Alternatively you could do a similar thing with typeinfo:

if(typeid(a) == typeid (A))
There are small subtle differences between the two but you get the idea.


Thankyou very much!
Jul 19 '05 #3

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

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.