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