Let's say I want to make a file parser which uses the strategy pattern to allow use of different specific parsers which would do all the hard work and then returned the results which could vary between them:
class FileParser
{
std::string file_path;
public:
NmeaFileParser(std::string file_path, ??? parser);
void parseFile();
??? getResults() {
return parser.getResults();
}
};
class Parser
{
public:
virtual void parseLine(std::string line);
??? getResults();
}
class SpecificParser : public Parser
{
void parseLine(std::string line);
SpecificFormat getResults();
}
And my problem is - how to correctly write such code? My first thought was templates, but I don't yet know how to use them, so as a second thought I got:
FileParser otherFunction() {
ExampleParser ep();
FileParser fp("example.txt", &ep);
return fp;
}
void function() {
FileParser fp = otherFunction(); // countains reference to deleted instance of ExampleParser
}
... but it's also buggy (I showed an edge case purposefully).
What's the proper way to structure that code?
Aucun commentaire:
Enregistrer un commentaire