Dear C++ Users,
I am trying to create a generic map container which shell contain data of different datatypes. I already found a solution in this forum here:
http://www.cplusplus.com/forum/general/14982/
Introducing a container with a Base Class as content type and inserting objectes of Derived Class types from that Base Class suites my implementation very well.
But it is not really working. I implemented it this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class MyType
{
public:
MyType() {}
virtual ~MyType() {}
};
template <class PT> class ParseType : public MyType
{
public:
ParseType( Parser<PT> cp ) : MyType(),
content( new Parser<PT>( cp ) ){}
~ParserType(){ delete content; }
PT get_val( int n )
{
return content->val( n );
}
Parser<PT>* content;
};
|
Then I insert one element.
1 2 3 4 5
|
// index being an object of type Parser<string>
ParseType<string>* test = new ParseType<string>( index );
// and index.val(0) = "-n"
iMap.insert( pair< string, MyType* >( index.id(0), test ) );
|
Now I think I should be able to call
1 2 3
|
const string key("-n");
iMap.at(key)->content->val(n);
|
Or
|
iMap.at(key)->get_val(n);
|
But neither one compiles with the error that "class MyType" (as the map container is pointing to a MyClass object) has no member/member function "content"/"get_val".
I also tried introducing member and member function "content" and "get_val" in "class MyType", which I thought should be hidden while constructing ParseType<string>. This would compile but it does not call the member "content or member function "get_val" from class ParseType.
A second try was to remove templatization of "class ParseType" and introduce a specific, let's say, "class ParseString" for objects of type Parser<string>. But the problems remain the same either the compiler complains due to missing member/member function or retreiving the map content will not call the derived class member/member function.
Can anyone help, please? Thanks in advance.
Cheers