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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:26:46+00:00 2026-05-12T08:26:46+00:00

Lets say I have a linked list with a bunch of different data in

  • 0

Lets say I have a linked list with a bunch of different data in it.

class Node
{
public:
    Node* next;
    AAA dataA;
    BBB dataB;
    CCC dataC;
};

Is there a way I make one iterator that would iterate over whatever variable I specify (rather than making three separate ones for each variable). I understand that the iterator could use templates to make it iterate over types AAA, BBB or CCC, but I don’t know how I could specify which variable to return.

  • 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-12T08:26:46+00:00Added an answer on May 12, 2026 at 8:26 am

    I think I’ve found a way to do pretty much what I want based on rstevens’ suggestion. I looked up some stuff on class member pointers and was able to skip the middleman accessor class by doing this:

    template <typename T>
    class iterator
    {
    private:
        Node *current;
        T Node::*var;
    
    public:
        iterator()
            : current(NULL), var(NULL) {}
    
        iterator(Node *start, T Node::*var)
            : current(start), var(var)
        {
        }
    
        typename T &operator *() const
        {
            return current->*var;
        }
    
        bool end() const
        {
            return (current == NULL);
        }
    
        iterator &operator++()
        {
            if (current)
                current = current->next;
            return *this;
        }
    };
    

    And then I modified Node to have convenience functions to make the iterators:

    class Node
    {
    public:    
        Node* next;
        AAA dataA;
        BBB dataB;
        CCC dataC;
    
        typedef iterator<AAA> AIter;
        typedef iterator<BBB> BIter;
        typedef iterator<CCC> CIter;
    
        AIter getAIter()
        {
            return AIter(this, &Node::dataA);
        }
    
        BIter getBIter()
        {
            return BIter(this, &Node::dataB);
        }
    
        CIter getCIter()
        {
            return CIter(this, &Node::dataC);
        }
    };
    

    So now I can do this to easily iterate over each data member of my class:

    for (Node::CIter iter = n1.getCIter(); !iter.end(); ++iter)
    {
        // tada!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say you have 2 classes: - Node - Doubly linked list (DLL) Your
Lets say have this immutable record type: public class Record { public Record(int x,
Let's say I have a class which, internally, stores a List of data: import
I have a linked list of structures. Lets say I insert x million nodes
Lets say I have two tables - Cat and Cat owner that are linked
Lets say I have an abstract base class and a derived class with some
lets say i have a class defined as shown below. Lets now say i
My problem is deleting a node from linked list. I have two structs :
Let's say I have a class A and this class has a List l
Ok so let's say we have a linked list of characters with a head

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.