- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个基类来定义所有派生类的接口(interface)。
我想要一个函数,允许读取该类的配置文件,使用 boost::property_tree
可以很顺利地完成。我们将此函数称为 readConfig
。这必须在每个派生类中定义,所以我将其设为纯虚拟。
我想重载基类中的 readConfig
函数,其中基类中的每个重载函数最终都会调用纯虚拟形式,例如:
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); // <= Calling pure virtual function!
}
基本上字符串版本只是纯虚拟形式的快速包装器。当我编译这个时,我得到一个错误:
no known conversion for argument 1 from std::string to boost::property_tree::ptree`
看来,非虚函数(来自 Base
)未被识别为可用。我检查了我的派生类定义是否正常:
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)
{
//...
}
请注意,我省略了很多 const
-correctnes、通过引用传递等,以使代码更具可读性。
我该怎么做才能解决这个问题?在非虚函数中使用纯虚成员函数是个好主意吗?
最佳答案
请参阅标题为“What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?”的 FAQ 项。可以在 FAQ 的许多镜像中找到,包括原始的 English version of the FAQ .
在发布之前检查常见问题解答通常是个好主意。
很有可能您还会发现标题为“好的,但是否有一种方法可以模拟该行为,就好像动态绑定(bind)在我的基类的构造函数?”,再加上如果这变得相关,那么您可能也有兴趣阅读 my blog article about the DBDI problem .
关于C++:在非虚函数中使用纯虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10465107/
我有一个特别的问题想要解决,我不确定是否可行,因为我找不到任何信息或正在完成的示例。基本上,我有: class ParentObject {}; class DerivedObject : publi
在我们的项目中,我们配置了虚 URL,以便用户可以在地址栏中输入虚 URL,这会将他们重定向到原始 URL。 例如: 如果用户输入'http://www.abc.com/partner ',它会将它们
我是一名优秀的程序员,十分优秀!