First of all, inheriting from standard containers is a very bad idea.
Secondly, I see many problems in your code:
Overuse of dynamic memory: You are using
new
in places where there is no reason at all. Note that your pair is ofstd::string
and int, that is, 12 bytes per pair with a commonstd::string
implementation (Asumming 32 bits architecture). 4 bytes of the pointer whichstd::string
uses to hold the char array, 4 bytes for the size counter of the string, and 4 bytes for the int of the pair. So there is no reason to use dynamic memory here. If you want to share ownership, use a smart_pointer or a reference wrapper likestd::reference_wrapper
.Following from the problem above, overuse of error-prone raw-pointers. As I said, smart pointers are a better alternative.
Finally, I think your compiler error is generated due to slicing: std::map
uses std::pair
internally to store the values. When you insert your pair, that pair is sliced, because std::map::insert
espects const std::pair&
as argumment and a reference of a derived class is casted implicitly to a reference of a base class.
So, as the compiler says, the pair (std::pair
) has no members inc()
and dec()
.
You are inheriting from std::pair
, not std::pair
is inheriting from your pair class.
Consider the tipical inheritance example:
class animal
{
void eat();
};
class dog : public animal
{
void bark();
};
The point here is the dog is an animal, but the animal is not a dog (Not all animals are dogs). So the dog can eat because it is an animal, and bark because its a dog. But the animal can't bark, because conceptually its not a dog.
So if you see a dog as if it was an animal only, the dog cannot bark.