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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:57:28+00:00 2026-06-15T21:57:28+00:00

So I have a class called List which stores a vector of pointers to

  • 0

So I have a class called List which stores a vector of pointers to classes of type Object. List has a function called add which initialises an Object class and adds it’s pointer to the vector. I thought that once the add function ended that the Object class would be destroyed and accessing the pointer would cause an exception. When I wrote a test program to test this it turned out that the Object class was never destroyed.

Are classes initialised inside a function ever destroyed once the function ends?

When are classes automatically destroyed?

  • 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-15T21:57:29+00:00Added an answer on June 15, 2026 at 9:57 pm

    Depends how you’re creating the object. If you are doing it like this:

    void add() {
      Object obj;
      vec.push_back(&obj);
    }
    

    Then you are creating obj with automatic storage duration. That means it will be destroyed when the add function ends. The pointer you have pushed into the vector will no longer point to a valid Object, so definitely don’t do this.

    You may, however, be doing this:

    void add() {
      Object* obj = new Object();
      vec.push_back(obj);
    }
    

    If you are, you are creating the Object with dynamic storage duration and it will not be destroyed at the end of the function. The pointer you push into the vector will remain valid. However, if you do this, you need to remember to delete the object at a later time. If you don’t, you’ll have a leak.

    The best option is to avoid using pointers at all, if you can. Just make the vector a std::vector<Object> and copy objects into it:

    void add() {
      vec.push_back(Object());
    }
    

    Or in C++11:

    void add() {
      vec.emplace_back();
    }
    

    If you really need pointers, prefer smart pointers.

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

Sidebar

Related Questions

I have the following class which stores a list of object arrays. public class
I have a class called Flamethrower which naturally has its own ammunition that is
I have a class called Property which has nothing but get -methods. All the
In C++, I have an Object class, from which a number of other classes
I have a class called ReportRequest as: public class ReportRequest { Int32 templateId; List<Int32>
I have a class with an integer variable called layer, there is a list
I have a list of objects called Activity: class Activity { public Date activityDate;
If I have a list of objects called Car: public class Car { public
I have a class called Trial which has_many results. Now What I want to
I have a class called Metadata, which is declared within the namespace A::B::C ,

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.