I am designing a class which will have a private member which is of type map<ID, Something>. I would like the user of the class to be able to iterate over every Something in the class but without ever being able to "see" the ID. The ID is important for other reasons.
Is there a way of doing this? I show some code below which should indicate what I am trying to achieve but I'm not sure how to implement begin() and end()
one way to do this would be slightly off-putting to some people (some folks have a hateful grudge against the technique):
- inherit from map into your own object. you would provide your own iterators and maybe the first() function so that they can't access the key from any path.
That stops the nice people, but its highly likely that your keys exist in memory and are reachable via pointer tricks from a less nice person. You can't let them call any base class stuff that would give access to the key!
So a better answer would be to encrypt the keys and let them alone, the user can see them but not do anything with them as-provided.
another way is 2 maps, one with the key and a fake key, and the other with data and fake key, aka parallel arrays style. The fake key is exposed, the real one is hidden in your private second map. Again, maybe possible to hack memory though. This is sort of standard on databases -- you don't want your keys to be data, but arbitrary 'account numbers' that are not associated with the entity, for privacy and security reasons.
> I would like the user of the class to be able to iterate over every Something in the class
> but without ever being able to "see" the ID. The ID is important for other reasons.
Provide a custom iterator that iterates (only) over the mapped_type.
Very nice ( I keep failing to get clever with iterators ; I just do not think that way), but to show what I was saying about being vulnerable to pointer hand waving... The offset (48) that works locally for me is wrong for the shell compiler, so you have to re-hack it to find the new offset for this compiler, but on mine the output is shown..
this is not a lot of effort to do (much more if its compiled int a lib with the guts hidden), and there isn't much you can do about it beyond some sort of encryption, if it matters to you. One of the flaws, or powerful tools, available in c++ is the ability to use pointers to do low level tasks, and unfortunately a side effect of that is the ability to circumvent normal OOP data protections.
this is not a lot of effort to do (much more if its compiled int a lib with the guts hidden), and there isn't much you can do about it beyond some sort of encryption, if it matters to you.
> The offset (48) that works locally for me is wrong for the shell compiler, so you have to re-hack it
> to find the new offset for this compiler, but on mine the output is shown.
The iterator ought to be a standard layout class type; so:
Right, very nice! I treated it as if I knew nothing (apart from the map template type, which I think can be fetched with RTTI?) about the target, and for some reason that made sense at the time but eludes me now, I went through it backwards.