This is probably a basic question for a regular C++ user. Functionally, I have an ECG monitor, and want to choose which output format to use at runtime. I have set up two classes that expose the same methods and members (ECGRecordingDefaultFormat and ECGRecordingEDFFormat) eg. ->InsertMeasure, ->setFrequency, ->setPatientName etc.
I know I could define one instance of each format class type, then put in:
if (ECGFormatToUse == ECGFormat.EDF) {
ecgDefaultFormat.InsertMeasure(x);
}
if (ECGFormatToUse == ECGFormat.Default) {
ecgEDFFormat.InsertMeasure(x);
}
all throughout the code, but I think I might not be using C++’s dynamic typing to its full extent.
The question is: can I define just one variable in main(), and after choosing the format I want at runtime, have the code use the right class with its exposed ‘InsertMeasure’ method, avoiding a whole lot of if/else’s throughout the code?
I’d be happy with just a reference to which aspect of inheritance/polymorphism (?) I should be using, and can google away the rest.
Thanks guys.
Pete
You can combine the factory pattern with C++’s polymorphism.