如果指针的子类的方法对于子类是唯一的,如何访问这些方法?

How do I access a pointer's subclass's methods if those methods are unique to the subclass?

本文关键字:方法 子类 何访问 访问 唯一 于子类 指针 如果      更新时间:2023-10-16

我的程序的一部分有两种可能的情况:(1(如果用户只给出2个命令行参数,则从标准输入(cin(中获取输入(2(如果用户给出3个命令行参数(最后一个是文件名(,则从文件中获取输入。为了不对两个选项重复使用相同的代码,我尝试使用指向 cin 和 ifstream 的超类的指针,istream 用于两种输入方式。

我的问题是,在下面代码的第 5 行和第 22 行,我试图引用仅适用于子类 ifstream 的方法(打开和关闭(。根据我的逻辑,如果调用这些方法,指针必须指向 if 流类型,但程序无法编译,因为这些方法未在 istream 类中定义。

有什么办法可以解决这个问题吗?

istream *currentStream;
if (argc == 3) {
// Handle optional file input
currentStream = new ifstream(argv[2]);
currentStream->open(argv[2]);
if (currentStream->fail()) {
cerr << "FILE COULD NOT BE OPENEDn";
return 1;
}
} else {
currentStream = &cin;
}
string myLine;
// go line by line and translate it
while (getline(*currentStream, myLine)) {
if (currentStream->eof()) {
break;
}
cout << rot13(myLine) << endl;
}
if (dynamic_cast<ifstream*>(currentStream)) {
currentStream->close();
}
// handle pointer
delete currentStream;
currentStream = NULL;
return 0;

通过获取缓冲区来动态分配std::cin的"副本"。将内存存储在std::unique_ptr中也是理想的选择,因为您不必担心手动删除指针。

#include <memory>
int main(int argc, char* argv[]) {
std::unique_ptr<std::istream> currentStream( argc == 3
? std::make_unique<std::ifstream>(argv[2])
: std::make_unique<std::istream>(std::cin.rdbuf())
);
// will only fail when the file cannot open
if (!currentStream) {
std::cerr << "FILE COULD NOT BE OPENEDn";
return 1;
}
std::string myLine;
// go line by line and translate it
while (std::getline(*currentStream, myLine)) {
std::cout << rot13(myLine) << std::endl;
}
}

有几个地方可以改进你的代码。我认为最突出的改进是你试图在一个功能中做太多。你不复制代码的目标是好的,但要模块化你的方法。将共享代码移动到其自己的函数,如下所示:

void do_stuff(std::istream & currentStream)
{
std::string myLine;
// go line by line and translate it
while (getline(currentStream, myLine)) {
if (currentStream.eof()) {
break;
}
std::cout << rot13(myLine) << std::endl;
}
}

此函数应包含两个代码路径之间共享的所有内容。(我将指针更改为引用,以便调用方立即知道空指针是不可接受的。当你改变你的主函数,让它调用这个函数时,你应该注意到一些事情变得更容易了。特别是,不需要动态分配(这导致没有尝试delete &cin- 这看起来很糟糕(。您可以轻松地将局部变量用于文件流。

int main(int argc, const char ** argv)
{
if (argc == 3) {
// Handle optional file input
std::ifstream fileStream(argv[2]);
fileStream.open(argv[2]);
if (fileStream.fail()) {
std::cerr << "FILE COULD NOT BE OPENEDn";
return 1;
}
do_stuff(fileStream);
fileStream.close();
} else {
do_stuff(std::cin);
}
return 0;
}

通过将公共代码移动到单独的函数,您可以保留在if子句中。无需推断*currentStream是否需要关闭,因为您从未离开创建该文件的代码分支。


还有另一个地方可以简化事情。不要打电话给openclose.您正在使用采用文件名的ifstream构造函数,因此构造函数已为您调用open。(显式调用open时,您告诉计算机关闭文件并重新打开它。同样,析构函数将为您调用close;这是RAII的一个关键点。

摆脱不需要的呼叫会离开:

int main(int argc, const char ** argv)
{
if (argc == 3) {
// Handle optional file input
std::ifstream fileStream(argv[2]);
if (fileStream.fail()) {
std::cerr << "FILE COULD NOT BE OPENEDn";
return 1;
}
do_stuff(fileStream);
// Keep in mind that, even though there is no C++ code here, there is something
// important being done after the call to do_stuff. Specifically, the destructor
// for fileStream is called, which closes the file for you.
} else {
do_stuff(std::cin);
}
return 0;
}

只需提取一个方法:

void process(std::istream is) {
string myLine;
// go line by line and translate it
while (getline(is, myLine))
cout << rot13(myLine) << endl;
}
int main(int argc, char** argv) {
if (argc == 3) {
ifstream ifs(argv[2]);
if (!ifs) {
cerr << "FILE COULD NOT BE OPENEDn";
return 1;
}
process(ifs);
} else {
process(cin);
}
}

Tas在评论中采用了正确的方法。你不能直接在 currentStream 上调用该方法,你必须在 cast 接口上调用它。

ifstream* stream = dynamic_cast<ifstream*>(currentStream);
if (stream) {
stream->close();
}

我还认为您可能应该更改代码以不依赖于动态强制转换,无论是新接口还是单独的方法。