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 8135709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:30:16+00:00 2026-06-06T10:30:16+00:00

I’m working with an awkward C library interface for a database containing a set

  • 0

I’m working with an awkward C library interface for a database containing a set of objects. The objects have a type and lets say that objects of type A contain a set of B objects and so on. Access to an object is via a handle which are defined as follows:

typedef struct
{
    int handle;
} AHandleT; 


typedef struct
{
    int handle;
} BHandleT;

To iterate over an A object’s B children, the following functions are used:

ReturnT getB(AHandleT /*in*/, BHandleT* /*out*/)
ReturnT getBNext(BHandleT /*in*/, BHandleT* /*out*/)

Likewise for iterating over a set of B objects:

ReturnT getC(BHandleT handle/*in*/, CHandleT* subHandle/*out*/)
ReturnT getCNext(CHandleT handle/*in*/, CHandleT* next/*out*/)

In order to work with this interface from C++ I have made the following iterator and I would appreciate your advice with regard to its implementation. Additionally, do you think this is a good approach? Keep in mind that I’m quite new to C++ and I will be writing my code using TDD.

template<class HandleT>
class HandleIterator 
{
public:
    typedef ReturnT (*GetNext)(HandleT, HandleT*);

    HandleIterator(): m_isLast(true)
    {
    }

    template<class ParentHandleT>
    HandleIterator(const ParentHandleT parentHandle, ReturnT (*getFirstChild)(ParentHandleT, HandleT*), GetNext getNext): m_isLast(false), m_getNext(getNext)
    {
        ReturnT rc = getFirstChild(parentHandle, &m_currentHandle);
        if(rc == NotExisting)
        {
            m_isLast = true;
        }
    }

    void operator++()
    {
        ReturnT rc = m_getNext(m_currentHandle, &m_currentHandle);
        if(rc == NotExisting)
        {
            m_isLast = true;
        }
    }

    void operator++(int)
    {
        ++(*this);
    }

    const HandleT& operator*() const
    {
        return m_currentHandle;
    }

    const HandleT* operator->() const
    {
        return &m_currentHandle;
    }

    friend bool operator==(const HandleIterator& left, const HandleIterator& right)
    {
        return left.m_isLast == right.m_isLast;
    }

    friend bool operator!=(const HandleIterator& left, const HandleIterator& right)
    {
        return !(left == right);
    }

protected:
    HandleT m_currentHandle;
    bool m_isLast;
    GetNext m_getNext;
};

Once I have a handle, I can then get the data contained within the object using functions of the following form, from the C interface:

ReturnT getAName(AHandleT)
ReturnT getBName(BHandleT)
ReturnT getBOnlyProprty(BHandleT)

But that’s the next stage.

etc.

  • 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-06-06T10:30:20+00:00Added an answer on June 6, 2026 at 10:30 am

    Your implementation is quite good, especially for a beginner.

    Just a couple of remarks:

    • In the constructor, why pass getFirstChild instead of providing the first child directly ?
    • The canonical signature for ++ are T& operator++() and T operator++(int)
    • == should compare more than m_isLast, otherwise it’s confusing. Cannot you compare the handles too ?

    Finally, when implementing an iterator, consider inheriting from std::iterator. It does not have any virtual method but provides the typedef that are usually expected in an iterator and will remind you that you need to pick a category: std::forward_iterator_tag here, most probably.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.