类继承调用不同的构造函数
嘿,我有一个 c++03 类,它有一个带有整数的简单构造函数。具有序列化方法的派生类应将文件名作为构造函数,从中加载整数,然后调用第一个构造函数。
class A {
public:
A(int foo);
}
和派生类:
class XmlableA : public A {
public:
XmlableA(int foo);
XmlableA(string xmlfilename) {
//load foo from xml
// call A::A(foo)
}
}
我尝试了一些不同的解决方案,但每次我得到
no matching function for call to ‘A::A()’
hei i have a c++03 class with a simple constructor that take an integer. And a derived class with serialization methods that should take a filename as a constructor, load the integer from it, and then call the first constructor.
class A {
public:
A(int foo);
}
and a derived class:
class XmlableA : public A {
public:
XmlableA(int foo);
XmlableA(string xmlfilename) {
//load foo from xml
// call A::A(foo)
}
}
i tried some different solution but every time i get
no matching function for call to ‘A::A()’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
几乎所有答案都是相同的,所以我会建议一个不同的解决方案,我个人更喜欢它。
将
static
成员函数Create
定义为:用法:
Almost all answers are same, so I would suggest a different solution, which I would prefer personally.
Define a
static
member functionCreate
as:Usage:
初始化它,如下所示:
您也可以考虑:
Initialize it, like so:
You may also consider:
在 C++ 中,基类是在子类之前构造的,因此您将无法执行此操作。您可以创建一个 Factory 来获取文件名并根据该文件中的内容创建一个对象。
示例:
然后这样称呼它:
有几件事需要注意。
XmltableA
类中的string xmlfilename
构造函数,因为如上所述,在调用基类的构造函数之前您无法知道 foo。auto_ptr
和shared_ptr
。In C++ the Base class is constructed BEFORE the Child class, so you will not be able to do this. You could make a Factory that takes a filename and creates an object based on what is in that file.
Example:
And then call it like so:
There are a few things to note.
string xmlfilename
cosntructor in theXmltableA
class because as discussed above, you cannot know foo before the base class's constructor is called.new
object and then make sure todelete
it when you're done with it.auto_ptr
andshared_ptr
.如果你想在调用
A::A(int)
之前做一些事情,你最终不得不破解,比如If you want to do something before the call to
A::A(int)
, you end up having to hack, something like好的,所以第一个很简单:
第二个需要执行类似于
我们可以实现的操作,
请注意
fooFromXML
,它加载 XML 文件并返回您需要的整数,必须 是静态的,因为当我们调用它时,我们还没有一个XmlableA
实例来调用它。对于多个参数(并且作为一般设计),工厂可能是最好的:如果您热衷于构造函数模型并且不关心效率,您可以这样做:
如果您担心重复解析 XML 文件,并且不关心重入,您可以通过将状态缓存在静态成员中来“记忆”
xFromXML
。OK, so the first one is easy:
The second one requires doing something like
which we can implement as
Note that
fooFromXML
, which loads the XML file and returns the integer you need, must be static, because when we call it we don't yet have anXmlableA
instance to invoke it on.For multiple arguments (and as a general design), the factory is probably best: if you're wedded to the constructor model and don't care about efficiency, you can do:
if you're concerned about parsing the XML file repeatedly, and don't care about re-entrancy, you can "memoize"
xFromXML
by having it cache state in a static member.如果您的类 A 没有默认构造函数,则必须在派生类的初始化列表中显式调用构造函数。
XmlableA(string fn) : A(readIntegerFromFile(fn)) {}
。但是,您应该考虑将序列化“外包”到一个单独的类中。例如,如果您有一个
A
类型的对象并且现在想要序列化它,会发生什么?您不能,因为您只能序列化XmlableA
。此外,如果您的客户决定不再需要 XML 序列化,而是需要 Yaml 或某种专有格式,会发生什么情况?您将必须更改所有代码。If your class A does not have a default constructor you have to explicitly call a constructor in the initialization list of your derived class.
XmlableA(string fn) : A(readIntegerFromFile(fn)) {}
.However, you should think about "outsourcing" the serialization into a separate class. E.g. what would happen if you have an object of type
A
and now you want to serialize it? You couldn't because you can only serialize aXmlableA
. Furthermore, what would happen if your client decides that he no longer wants a XML serialization but Yaml or some proprietary format? You would have to change all your code.