#include <iostream>
    #include <string>
    #include <utility>
    #include <map>
    using namespace std;
    class MyPair: public pair<string, int>
    {
        int _ref;
    public:
        MyPair(): pair<string, int>(), _ref(0) {}
        MyPair(string arg1, int arg2): pair<string, int>(arg1, arg2), _ref(0) {}
        ~MyPair();
        void inc() {
            _ref++;
        }
        void dec() {
            _ref--;
            if (_ref == 0) delete this;
        }
    };
    class MyMap: public map<string, int>
    {
    public:
        MyMap(): map<string, int>() {}
        MyMap(const map<string, int>& mp): map<string, int>(mp) {
            //for(auto i=begin(); i!=end(); ++i) i->inc();
            //I want to perform that instruction above, but it gives me an error
        }
        ~MyMap() {
            //for(auto i=begin(); i!=end(); i++) i->dec();
            //same as here
        }
        void insertNewPair(MyPair * mypair) {
            insert(*mypair);
            mypair->inc();
        }
    };
    int main(int argc, char **argv)
    {
        MyMap mymap;
        mymap.insertNewPair(new MyPair("1", 1));
        mymap.insertNewPair(new MyPair("2", 2));
        cout << "mymap[\"1\"] = " << mymap["1"] << endl;
        cout << "mymap[\"2\"] = " << mymap["2"] << endl;
        return 0;
    }
I subclassed a class from std::pair so that I can append a reference counter in it. I named it "MyPair". I also subclassed from std::map and I named it "MyMap". So that everytime I insert new MyPair in MyMap, it calls the inc() member function of MyPair, so that MyPair would increment its _ref member which is the counter of its reference. If I delete an instance of MyMap, it decrements all the _ref member function of each of its contained MyPairs. If _ref in MyPair hits 0, it means that it was no longer referenced, so it will delete itself.
That code above works, because I managed to comment some lines of codes there in MyMap. When I uncomment them, the compiler gives me an error that says that std::pair has no members like inc() and dec(), even though I insert MyPair instances there at the main function. I know that the compiler doesn't notice that I inserted MyPair instances that contains those members, not just a plain std::pair.
Is there's a way that I can call those members of MyPair(inc() and dec()) inside MyMap? Thanks for your answers in advance.
 
     
     
    