In my C++ audio project I have the following scenario. I have a virtual class called…
- User
In my C++ audio project I have the following scenario. I have a virtual class called ScoreReader. Inside it, I have the following function: void ConfigureParameters() { InitializeParameters(); // the rest of the function } InitializeParameters is pure virtual function, that in my opinion should look like something similar in every derived class: void InitializeParameters() final { AddParameters({"T", "SR"}); } // this line comes from a derived class With void AddParameters(const std::initializer_liststd::string ¶ms) defined again in ScoreReader. My concern is linked to if this syntax is the simplest one or if I am overkilling the code. Moreover, I would that InitializeParameters looks how the code I provided in each of the derived class, for this reason I am considering if I am able to reinforce this situation. Please provide feedback on these two doubts.
- Assistant
- User
Ok, I forgot to explicit that T and SR are parameters owned by the derived class. They are owned by the derived class and there is no guarantee that other derived classes will have the same parameters. For this reason, I am keeping this structure.
- Assistant
Thank you for clarifying that
Tand are specific to certain derived classes and not universally used across all derived classes. In this case, your existing structure where is a pure virtual function makes sense, as it allows each derived class to specify its own unique set of parameters. - User
An alternative can be to defne something like void InitializeParameters() { std::initializer_liststd::string params = retrieveParameters(); AddParameters(params); } In the base class, while inserting retrieveParameters() as a function that reads the parameters from a private member. Does it sound feasible or to rely on the implementation of a private member is a bad practice?
- Assistant
- User
There is some way to verify that in the derived class a private member is present? I am thinking using a static assertion.
- Assistant
- User
I am concerned about this point, consider my implementation of AddParameters in the base class: void AddParameters(const std::initializer_liststd::string ¶ms) { for (const auto ¶m : params) { // do something parameters_[param] = 0.; } } Here parameters_ is a std::unordered_amp<std::string, double> and I think I am relying on the fact that also the derived class should have this map because it is private in the base class, so it is not accessible from the derived class. Please provide feedback on this concern.
- Assistant