c++ - 还有另一种方法来显式初始化父类吗?
问题描述
在子类中完成一些更改后,我正在寻找一种方法来启动我的父类。
前任:
class Parent {
public:
Parent(stream f) {
//some code
}
};
class Child : public Parent {
public:
Child(string fileName) : Parent(???) { // Line 10
//some code
}
};
在第 10 行,我应该给出使用“fileName”创建的 fstream。我在哪里可以创建这个流变量?
解决方案
评论已经显示了一种可能的方式,所以你可以这样做:
#include <fstream>
#include <string>
class Parent {
public:
Parent(std::fstream f) {
// some code
}
};
class Child : public Parent {
public:
// Do it like this:
Child(std::string fileName) : Parent(std::fstream(fileName)) {
// some code
}
};
还有一件事:Parent
显然需要是 的直接基类Child
,所以我相应地修改了代码。否则它不会编译。
推荐阅读
- proxy - Conda keeps trying to connect to a proxy even though it should be desabled
- java - Correct way of field initialisation in static code block in Java
- sql - How to get distinct rows based on multiple columns are include the latest row from another table?
- javascript - 如何获取我单击的特定表单的输入字段的值,所有表单及其输入具有相同的类但值不同
- python - 我可以在忽略黑色像素的情况下迭代图像吗?
- javascript - 当我从下拉列表中选择特定列表项时,我想调用 JavaScript 函数
- mvvm - 为注册模块使用 1 个或多个视图模型(登录 - 注册 - 重置通行证)?
- python - How to separate a dataset into more than 2 random samples
- python - statsmodel 获取 resid_studentized_external
- c++ - SQLConfigDataSource 不为 64 位驱动程序添加系统 DSN