Essentially, what I'd like is for the value
object to maintain a reference to the corresponding key
object, because there's some useful information in there, which would be nice to access via the value
object.
What I'm attempting to do may just not make sense, but consider the following:
class key
{
// ... Various members ...
friend bool operator< (const key &lhs, const key &rhs) { /* ... */ }
};
class value
{
public:
value(const key &k) : k(k) {}
private:
const key &k;
// ... Various members ...
};
std::map<key,value> m;
// start a new scope, which may be due to e.g. function call, loop, etc.
{
key k; // I'm on the stack!
m.insert(std::pair<key,value>(k, value(k)));
}
Of course, this doesn't work, because this is a reference to a stack object, which breaks as soon as k
goes out of scope. Is there somehow a way to get a reference back to the copy of the key maintained in the map
?