new syntax for associative array

Helo,

here is the thing: i made an implementation of the associative array in C++. It works well, but i want to make it's syntax closer to that of the original array's.

Lets say, that Set(<Key>,<Value>) sets <Value> to <Key>, and that Value(<Key>) gives back the Value, that is associated with <Key>.
What i want to do is being able to write
array[<Key>] = <Value>; instead of array.Set(<Key>,<Value>) and
<Value> = array[<Key>]; instead of <Value> = array.Value(<Key>)

I thought about using define to define a macro that does this, but macros only work in the same file where they are. The Associative array is however in a header file, wich needs to be included where it is used.
Implement an operator:
1
2
3
4
5
 //...
 <Value> operator[] (<Key>)
{
    //do as in Value(<Key>);
}

(I'm assuming that you are using a class)
Last edited on
Wow, it really works. I never tought that you can do this with operators. Thanks. Everyday i learn something new.
Topic archived. No new replies allowed.