Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi!
How does it work with hash codes and equality in native c++ if you e.g.
compare with Java or C# where all classes inherits class Object (there this methods is declared)?

Is there any built in functions to do this in c++ or have you to make such functionality yourself? Or how can you then implement e.g. a directory without hash code with complexity O(1) for a find() or insert() method???

Thanks in advance, WaZoX

EDIT: with directories, map and set I mean the more general data structures , I don't know what they are called in c++.
Posted
Updated 2-Sep-10 10:11am
v2

The sort of things you're likely to override from Object in Java you're more likely to implement as operators on a class by class basis in C++. So if I wanted a custom test for equality in C++ I'd define and implement operator==:

class A
{
    public:
        bool operator==( const A &compare_with )
        {
            // Do comaparison in here...
        }
};


[In C++ you're more likely to implement operator<() instead, but the principle is the same].

The C++ standard library, as far as I know, doesn't provide any support for hashing. The original SGI STL that a fair chunk of the standard library provides some support for hashing of standard library and builtin types. These are usually implemented as template functors (classes that implement operator()) so they're not intrusive (as in clogging up the class's interface) the way they are in Java. So if you want a hash function that operates on strings you'd use:

hash<string> string_hash;


but in most cases you'd just use anonymous objects.

Cheers,

Ash
 
Share this answer
 
v3
Comments
WaZoX 3-Sep-10 12:28pm    
Think I got a better picture of it now. I going from Java programing to c++ so some questions come up =), thank you very much.
In the meantime we have arrived at c++0x which incudes unordered_map and some other support for hashing. std::hash has got some default implementation for hashing functions. Microsoft's latest implementation includes all (and more) of this in std:: as defined by c++0x, the boost based implementations are still in std::tr1...
 
Share this answer
 
try boost and/or cryptopp

Boost website[^]

CryptoPP homepage[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900