My declaration was unordered_map<Comparison,int> Chs when Comparison is a class name.
Now I have few questions;
A. How can I insert, when the constructor called, some elements (like done with vector) ?
e.g. :
unordered_map<Comparison,int> Chs =
{
(new Equal_to<int>,10),
(new Not_equal_to<int>,30),
(new Greater<int>,20)
};
this code get now compile error.
B. How to free the memory that allocated (with new) in this declaration?
C. In inherit class:
template <class T,class V>
class RC : public unordered_map<T, V>
How should I write the RC constructor, so its can initialize with elements like A question?
Thank you, and sorry about my poor English.
You’d rather not inherit from standard containers, they weren’t designed for it.
Also, as mentioned, to have the contained Comparisons behave polymorphically, you need to either store references or pointers. Pointers are much simpler to work with.
In order to avoid having to worry with freeing up the memory, use smart pointers (std::unique_ptr or std::shared_ptr).
The following snippet shows the workings of most of the above, including how to do uniform initialization on your own container class (
RC<>):In the department of (likely useless optimization, you can have a perfect forwarding constructor if it doesn’t conflict with your other constructor needs. This would have the benefit of supporting move semantics correctly: