I have a class which looks something like this. I’d prefer to have the typedef of ParentMember in the Parent class and rename it Member. How might this be possible? The only way I can see is to have std::vector as a public member instead of using inheritance.
typedef std::pair<std::string, boost::any> ParentMember;
class Parent: public std::vector<ParentMember>
{
public:
template <typename T>
std::vector<T>& getMember(std::string& s)
{
MemberFinder finder(s);
std::vector<ParentMember>::iterator member = std::find_if(begin(), end(), finder);
boost::any& container = member->second;
return boost::any_cast<std::vector<T>&>(container);
}
private:
class Finder
{
...
};
};
Yeah… that’s The Correct Way™.
Now let’s assume the class in question is not
std::vectorbut something else, which would make this a legitimate question.One solution is to use the non-
typedefversion for the base class and thentypedefitIn case of
std::vectoras a base class, it even exposes the type as a nestedvalue_typetypedef, which would allow you to make sure the two are never different:If your
Parentclass is a template itself, you would need