我如何访问一个指针子类方法,那些方法是子类独有的?

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

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

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

中定义

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

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 OPENED\n";
            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;

Tas在评论中有正确的做法。不能在currentStream上直接调用方法,必须在cast接口上调用。

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

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

通过获取其缓冲区动态分配 std::cin 的 "copy"。将内存存储在 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 OPENED\n";
    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;
    }
}

此函数应包含两个代码路径之间共享的所有内容。 (我将指针更改为引用,以便调用者立即知道空指针是不可接受的。)当您更改 main 函数以便它调用这个函数时,您应该注意到一些事情变得更容易了。特别是,不需要动态分配(这导致没有尝试 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 OPENED\n";
            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 OPENED\n";
            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 OPENED\n";
            return 1;
        }
        process(ifs);
    } else {
        process(cin);
    }
}