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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:44:44+00:00 2026-06-04T22:44:44+00:00

this is my first time using the list STL and i’m not sure if

  • 0

this is my first time using the list STL and i’m not sure if what i’m trying to do is possible.
I have class_B which holds a list of class_A, I need a function in class_B that takes an ID, searches the list for an instance with the same ID, and gets a pointer form the list to the instance in that list:

bool class_B::get_pointer(int ID,class_A* pointer2A){

   list<class_A>::iterator i;
   for(i=class_A.begin();i!=class_A.end();i++){
       if((*i).get_id()==ID) {
           \\pointer2A=(i);<---------------this is what I'm trying to do
           return true;
       }
   }
   pointer2A=NULL;
   return false;
}

how do I perform this, is it possible to convert from iterator to instance ?

EDIT:

I’m using this function in a multi-threaded program and I can’t return an iterator to the calling function since another thread might delete an element of the list.

Now that I have a pointer to my element(and lets say it’s locked so it can’t be deleted), and a different thread removed another element and performed a sort on the list, what will happen to the pointer I’m holding ? (I don’t know how the list rearranges the elements, is done by copying the elements using a copy c’tor, or by another mean?).

Useless answer was the most helpful in my case (BIG thanks), and yes I should use a reference to the pointer since I’m planing to change it.

  • 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-04T22:44:45+00:00Added an answer on June 4, 2026 at 10:44 pm
    if(i->get_id()==ID) {
        pointer2A=&*i;
        return true;
    }
    

    iterators are designed to have similar semantics to pointers, so for example you can write i->get_id() just as if you had a pointer to A.

    Similarly, *i yields a reference A&, and &*i converts that back into a pointer – it looks a bit clunky (it would be an identity operation if i were really a pointer), but it’s idiomatic.

    Note that this won’t do what you presumably want anyway – the caller’s class_A* pointer2A is passed by value, so only get_pointer‘s copy of the pointer is modified, and the caller won’t see that value. Try this:

    bool class_B::get_pointer(int ID, class_A *& pointer2A)
    {
        list<class_A>::iterator i;
        for(i=class_A.begin();i!=class_A.end();i++) {
            if(i->get_id()==ID) {
                pointer2A=&*i;
                return true;
            }
        }
        pointer2A=NULL;
        return false;
    }   
    

    Now pointer2A is passed by reference, so the caller’s copy gets modified inside your function.

    BTW, you can read the parameter declaration class_A * & pointer2A right-to-left, as “pointer2A is a reference to a pointer to class_A”.

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

Sidebar

Related Questions

This is my first time using stackoverflow for a question, I have read a
Ok, I'm trying to get this working. This is my first time using LINQ.
This is the first time I'm using jqgrid. I have confused in how to
I am trying to use data() for the first time. I have a list.
This is my first time using Eclipse 3.3.2 to make an application in Java.
This is my first time using JFileChooser in Java and I'm currently attempting to
This is my first time using stackoverflow for a question that I need to
This is my first time using mysql and I am tying to learn how
This is my first time using Store Kit and everything has gone great up
this is my first time using StAX for parsing XML documents (still in the

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.