cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : new syntax for associative array
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  new syntax for associative array

kanesoban (37)
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.
|
Bazzy (539)
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
kanesoban (37)
Wow, it really works. I never tought that you can do this with operators. Thanks. Everyday i learn something new.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us