gpt4 book ai didi

c++ - 避免 while (!is_eof)

转载 作者:行者123 更新时间:2023-11-30 04:29:43 27 4
gpt4 key购买 nike

我正在使用 (C++) 库,其中需要使用流初始化对象。库提供的示例代码使用此代码:

// Declare the input stream
HfstInputStream *in = NULL;

try
{
// This sets up the special stream object for the later object (see below)
in = new HfstInputStream("pathToFile.hfsto");
}
// catch errors...
// {omitted}

// Initialize the object
while (not in->is_eof()) {
if (in->is_bad())
{
std::cerr << "ERROR: Stream cannot be read." << std::endl;
exit(1);
}
// Here we initialize the object using the stream
HfstTransducer t(*in);
}

我的问题是,由于作用域的原因,这个对象不能在 while 循环之外使用。我必须用流声明它(据我所知),所以我不能在循环内用流声明然后初始化它。

我的问题是 (1) 我错了吗?我真的可以以某种方式在循环外声明它吗? (2) 是否有另一种(更好的)方法可以完全避免循环。例如,如果我要使用 try/catch 并捕获异常。

我是 C++ 的新手,希望找到最佳实践,所以请让我知道是什么。谢谢。

另外,需要明确的是,我希望创建一个类来使用该对象的持久版本,这样我就不必在每次需要使用这些对象时都不断地创建/销毁它们。

附言:here's a link to the documentation for the object if it is relevant

编辑:如果我尝试在循环外声明变量然后初始化它,我会得到一个错误

HfstTransducer t;
while (not in->is_eof()) {
t(*in);
}
// ERROR: main.cpp:47:0 main.cpp:47: error: no match for call to '(hfst::HfstTransducer) (hfst::HfstInputStream&)'

我是否试图错误地初始化它?

最佳答案

为了实现您的需要,您必须有一个指向在 while 范围之外声明的对象的指针,如下所示:

//Declare the pointer to the object outside the while score. This way it will be available to you even outside of the while
HfstTransducer* t = 0;
// Initialize the object
while (not in->is_eof()) {
if (in->is_bad())
{
std::cerr << "ERROR: Stream cannot be read." << std::endl;
exit(1);
}
// Here we initialize the object using the stream only if it's not already been initialized before
// thanks to Necrolis for the observation
if(t == 0)
t = new HfstTransducer(*in);
}

这将声明并初始化一个 HfstTransducer 类型的对象到堆中。这意味着析构函数在您离开作用域后不会自行调用,但您必须通过调用显式调用它:

delete t;

编辑:回答关于指针与普通对象的一般问题:

这是C/C++非常重要的一部分。一篇更好解释这个的文章可以看here .

如果我要用几句话来解释它:

HfstTransducer t;

您正在声明该类型的对象并将其放入堆栈中。它的生命周期只持续到范围结束。您无法在作用域外访问它,因为它的析构函数将在作用域结束后立即调用。

另一方面

HfstTransducer*t = new HfstTransducer();

将t初始化为HfstTransducer类型的对象,并放入堆中。堆是什么,引用上面那篇文章,基本上就是操作系统分配给你的程序的内存。在 C++ 中,您使用运算符 new 请求堆中的内存,并使用 delete 运算符释放它。在 C 中,您可以使用 free()malloc() 函数实现相同的效果。

因此,除非您显式调用其析构函数,否则堆中的某些内容在整个程序运行期间都处于事件状态。正如您在示例中通过调用 delete t;

所做的那样

否则会导致所有 C/C++ 程序员都必须面对的问题,即所谓的内存泄漏。这基本上是您认为空闲的内存,但不是因为您忘记删除/释放它。

关于c++ - 避免 while (!is_eof),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9172614/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com