questions

G

Guest

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"] = "(e-mail address removed)";
-----

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
 
R

Ron Natalie

1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:

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

Guest

1. In Java there is a class named HashTable. Is there something similar in C++?
I mean something like:

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!
 

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,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top