Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: use a pure virtual function in a non-virtual function
    text
    copied!<p>I'm trying to make a base class that defines the interfaces for all derived classes. </p> <p>I'd like to have a function that allows one to read a config file for that class, which is working out pretty smooth using <code>boost::property_tree</code>. Let's call this function <code>readConfig</code>. This would have to be defined in each derived class, so I made it pure virtual.</p> <p>I would like to overload the <code>readConfig</code> function in the base class where every overloaded function in the base class eventually calls the pure virtual form, e.g.:</p> <pre><code>class Base { // ... void readConfig(string, string); // read config from file virtual void readConfig(boost::property_tree::ptree, string) =0; // read config from ptree } void Base::readConfig(string filename, string entry) { boost::property_tree::ptree pt; read_xml(filename, pt); readConfig(pt, entry); // &lt;= Calling pure virtual function! } </code></pre> <p>Basically the string version is just a quick wrapper for the pure virtual form. When I compile this, I get an error:</p> <pre><code>no known conversion for argument 1 from std::string to boost::property_tree::ptree` </code></pre> <p>So it seems, the nonvirtual function (from <code>Base</code>) is not recognized as being available. I checked that my derived class definition is ok:</p> <pre><code>class Deriv : public Base { // ... void readConfig(boost::property_tree::ptree, string); // implement virtual, error is on this line } void Deriv::readConfig( boost::property_tree::ptree pt, string entry) { //... } </code></pre> <p>note that I omitted a lot of <code>const</code>-correctnes, pass by reference etc. to make the code a bit more readable. </p> <p>What can I do to fix this? Is using a pure virtual member-function in a non-virtual function a good idea?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload