Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In order to achieve what you need, you have to have a pointer to the object declared outside of the while scope like that:</p> <pre><code>//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-&gt;is_eof()) { if (in-&gt;is_bad()) { std::cerr &lt;&lt; "ERROR: Stream cannot be read." &lt;&lt; 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); } </code></pre> <p>This will declare and initialize an object of HfstTransducer type into the heap. This means that the destructor will not be called on its own after you leave scope but that you have to explicitly call it by calling:</p> <pre><code>delete t; </code></pre> <p>EDIT: To answer to your general question about pointer vs normal object:</p> <p>This is a very important part of C/C++. An article to better explain this can be seen <a href="http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/" rel="nofollow">here</a>.</p> <p>If I were to explain it in a few words by doing:</p> <pre><code>HfstTransducer t; </code></pre> <p>You are declaring an object of that type and putting it in the stack. Its life-time lasts ONLY until the end of the scope. You are not able to access it out of scope, since its destructor will be called as soon as the scope ends.</p> <p>On the other hand</p> <pre><code>HfstTransducer*t = new HfstTransducer(); </code></pre> <p>initializes t as an object of HfstTransducer type and places it in the heap. For what the heap is refer to the article above, but it basically is the memory allocated to your program by the operating system. In C++ you ask for memory in the heap with the operators <em>new</em> and free it with the <em>delete</em> operator. In C you achieve the same with the <em>free()</em> and <em>malloc()</em> functions.</p> <p>So something in the heap is alive for the whole duration of your program unless you explicitly call its destructor. As you can do in the example by invoking <em>delete t;</em></p> <p>Failure to do so leads to what all C/C++ programmers have to face, the so called memory leaks. Which is basically memory you thought is free but is not because you forgot to delete/free it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload