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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:47:52+00:00 2026-06-04T10:47:52+00:00

My class has a pointer vector: ptr_vector<Class> vec; And in some setup method adds

  • 0

My class has a pointer vector:

ptr_vector<Class> vec;

And in some “setup” method adds a few classes to the vector:

void setupOrSomething()
{
    vec.push_back(new Class(...));
    ....
}

Now clients of this class may wish to add their Class objects to this classes list:

void addThingToMyList(Class *cPointer)
{
    vec.push_back(cPointer);
}

And they may wish to remove them by passing the same pointer:

void removeThingFromMyList(Class *cPointer) { ... }

Now if I understand correctly, after reading this answer (https://stackoverflow.com/a/357043/48998), I need to implement that method as follows:

void removeThingFromMyList(Class *cPointer)
{
    vec.release(std::find_if(vec.begin(),vec.end(),CheckPointerValue(cPointer)).release();
}

struct CheckPointerValue
{
     CheckPointerValue(Class* c):cptr(c) {}
     bool operator()(Class const& X)    { return &X == cptr;}
     private:
        Class* cptr;
};

I understand I have to also call release() a second time on the auto_ptr that is returned from the ptr_vector.release().

Am I correct in assuming this will ensure that the caller of this method (RemoveThing…) will retain a valid reference to its Class object and that it will not be deleted? I simply want vec to gain a temporary ownership and then relinquish 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-04T10:47:54+00:00Added an answer on June 4, 2026 at 10:47 am

    Yes, they will retain a pointer to a valid instance. Of course they need to know that the pointer refers to a valid instance in the first place AND that a pointer to that instance is stored in the vector. If it’s not, you will get undefined behavior and probably a seg fault.

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

Sidebar

Related Questions

I have a class which has a std::vector of child control pointer. For obvious
I have a Snake class, which has a pointer to a Controller class object.
String class has a constructor: new String(byte[] bytes, Charset charset) and a method: byte[]
I have a C++ template class that contains a method pointer and a class
I've a class Foo<T> which has a vector of smart pointers to Shape derived
I have a base class which has two child classes derived from it. class
I have a class that has a vector of another class objects as a
I have a class named Texture that has a pointer to a SDL_Surface. When
My base class has properties that are used by a subclass. where should Release
A TimeSheetActivity class has a collection of Allocations. An Allocation is a value object

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.