Re: hasMember
On Dec 8, 8:19 pm, Jonathan Lee <jonathan.lee....@gmail.com> wrote:
On Dec 8, 5:38 am, Andrea Crotti <andrea.crott...@gmail.com> wrote:
James Kanze <james.ka...@gmail.com> writes:
The simplest solution is to return a pointer to the map's
contents, or NULL if the value isn't there, e.g.:
int const* Cont::getValue(int idx) const
{
std::map<int, int>::const_iterator result
= content.find(idx);
return result == content.end()
? NULL
: &result->second;
}
and:
int const* i = obj.getValue(idx);
if ( i != NULL ) {
...
}
--
James Kanze
I would do that in C but I don't think it looks nice in C++...
Would it be more acceptable with a typedef?
class Cont {
public:
typedef const int* const_iterator;
const_iterator end() const { return NULL; }
const_iterator getValue(int idx) { ... }
};
...
Cont::const_iterator it = obj.getValue(idx);
if (it != cont.end()) {
...
}
--Jonathan
Though, since const int* has arithmetic operators defined on it
you may not to use the bare pointer. Anyway, the point is that
this really isn't that unusual in C++. The iterator for
std::vector is probably a simple pointer, hidden with a typedef.
--Jonathan