views:

57

answers:

3

I'm trying to make a wrapper to the STL map container, in order to add a const method to return the value given the key. In map, operator[] isn't const, and find() requires dereferencing to get the value (map.find()->second). I'm basing some of my "research" off of http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-const-map by the way.

The code so far (all inside a single header file):

#include <map>
template <typename K, typename V>
class easymap : public std::map<K, V>
{
    //Constructor
    easymap() : std::map<K, V>() {};

    //The get method
    V get(K key)
    {
        std::map<K, V>::const_iterator iter(find(key));
        return iter != end() ? iter->second : V();
    }
};

When I try to compile this, I get the following errors:

In member function `V easymap::get(K)':
    expected `;' before "iter"
`iter' was not declared in this scope
there are no arguments to `end' that depend on a template parameter, so a declaration of `end' must be available|
(if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

Does how I'm trying to go about doing this make sense? If so, how do I make this work? If not, how would I go about achieving the effect I'm looking for?

+1  A: 

You are missing the template parameters for map, you have to specify typename when declaring the iterator (see here), and for some reason unknown to me (probably a namespace conflict) you have to use this when calling end():

template <typename K, typename V>
class easymap : public std::map<K,V>
{
    //Constructor
    easymap() : std::map<K, V>() {};

    //The get method
    V get(K key)
    {
        typename std::map<K, V>::const_iterator iter(find(key));
        return iter != this->end() ? iter->second : V();
    }
};
Aleph
Alf P. Steinbach
@Aleph: Thanks so much, that worked perfectly! Good information on typename, something I definitely should have read up on by now, as well as the tip to use this->end(), which I agree, makes little sense. G++'s error message on that doesn't help a whole lot either (to me at least)
Lewis
@Alf: Ah, that helps clear that up. Thanks!
Lewis
+1  A: 

It's no quite good idea to inherit an STL container.. None of the STL containers is supposed to be a base class. You must really have a good reason to do that and to be very careful using such objects. The reason is, that none of the STL containers have a virtual destructor. So, if you have a pointer, for example std::map<..> *, that points to your object (that has inherited the map container), the one of the destructors will NOT be called. This is a 100% memory leak.

EDIT: http://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-rather-than-delegate <- very useful topic about that

Kiril Kirov
How often do you dynamically allocate an STL container?
Alf P. Steinbach
Yeah, right, but it's still a potential problem.
Kiril Kirov
+1  A: 

Do not derive from std::map. Rather, wrap a std::map instance in your easymap, following the composition before inheritance principle. Besides of all the technical reasons, this reflects the design intent much better: provide a simplified API to map hiding the default one:

template<typename K, typename V>
class easymap {
 std::map<K, V> mMap;
public:
  V Get(K Key) const {
   // ...
  }
};
paul_71