Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:30:50+00:00 2026-05-29T06:30:50+00:00

I’m working with a (C++) library, where the object needs to be initialized with

  • 0

I’m working with a (C++) library, where the object needs to be initialized with a stream. The sample code provided with the library uses this code:

// 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);
}

My problem is that this object cannot be used outside of the while loop because of scoping. and I have to declare it with the stream (as far as I can tell), so I can’t declare and then initialize it with the stream inside the loop.

My questions are (1) am I wrong? Can I actually declare it outside the loop somehow? (2) is there another (better) way to do this that avoids the loop altogether. For example, if I were to use try/catch and catch the exceptions.

I’m very new to C++ and looking to find out best practices, so please let me know what’s what. Thanks.

Also, to be clear, I’m looking to make a class that would use a persistant version of this object so I don’t have to constantly create/destroy these objects every time I need to use them.

PS: here’s a link to the documentation for the object if it is relevant

EDIT: If I try to declare the variable outside the loop and then initialize it I get an error

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&)'

Am I trying to initialize it incorrectly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:30:51+00:00Added an answer on May 29, 2026 at 6:30 am

    In order to achieve what you need, you have to have a pointer to the object declared outside of the while scope like that:

    //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);
    }
    

    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:

    delete t;
    

    EDIT: To answer to your general question about pointer vs normal object:

    This is a very important part of C/C++.
    An article to better explain this can be seen here.

    If I were to explain it in a few words by doing:

    HfstTransducer t;
    

    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.

    On the other hand

    HfstTransducer*t = new HfstTransducer();
    

    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 new and free it with the delete operator. In C you achieve the same with the free() and malloc() functions.

    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 delete t;

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.