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

  • Home
  • SEARCH
  • 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 8911673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:00:26+00:00 2026-06-15T04:00:26+00:00

Is it possible to remove an object from std::list using .remove and the object

  • 0

Is it possible to remove an object from std::list using .remove and the object as a pointer?

Im getting quite confused with pointers.
I have the following class for example:

class AsteroidGen{
    public:
        std::list<Asteroid*> listAsteroids;
        void AsteroidGen::generateAsteroid(int amount, int delet);
        void AsteroidGen::DrawAsteroids();
};

That has a list of pointers to my asteroid object.
Since it is a pointer I assume I would be able to go through it using pointers and delete it.

In my main class I create an instance of AsteroidGen and populate the list and draw it. This all works fine. I then try to remove one of the items from the list that my bullet has collided with:

bool CheckBulletCollision(Lazer lazer, AsteroidGen asteroids){
    bool collision = false;
    for each(Asteroid *c in asteroids.listAsteroids){
        Position pos = c->pos;
        float lazx = lazer.mPos.x;
        float lazy = lazer.mPos.y;
        float lazz = lazer.mPos.z;
        float distance = sqrt(pow(pos.x - lazx, 2) + pow(pos.y - lazy, 2) + pow(pos.z - lazz, 2));
        if( distance < 2.05){
            asteroids.listAsteroids.remove(c);
            lazergone = true;
            break;
        }
    }
    return collision;
}

i tried going line by line through my code to see what is going wrong, but the list item is not actually removed in the next frame. I am probably not using pointers correctly, I still find them difficult.

Could someone give me advice please!
Tell me if I need to add more code, I assumed this would be sufficient
Thank you.

  • 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-15T04:00:27+00:00Added an answer on June 15, 2026 at 4:00 am

    You’re passing AsteroidGen into the function by value. This means a copy of it is made and the function works on the local copy, which is discarded when the function ends. The asteroid is indeed removed from the list, but from the list stored in the copy.

    If you want to modify the AsteroidGen object in the function, you must pass it by reference:

    bool CheckBulletCollision(Lazer lazer, AsteroidGen &asteroids) {
      // ... rest of code as before
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ Vector Pointers to Objects Does std::list::remove method call destructor of each
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Is it possible to remove a decorator from an object? Say I have the
I'm trying to remove a specific graph object from the zedgraph, is it possible....?
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
Possible Duplicate: Remove handlers on disposing object I have some code which dynamically adds
Is it possible to remove just the translation element from a Matrix object so
Possible Duplicate: How to remove a property from an object? I am comparing two
Possible Duplicate: How to remove a property from a javascript object JavaScript Hashmap Equivalent
I wonder if it's possible to add an object from one list to another

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.