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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:41:47+00:00 2026-06-01T17:41:47+00:00

Basic Question: when does a program call a class’ destructor method in C++? I

  • 0

Basic Question: when does a program call a class’ destructor method in C++? I have been told that it is called whenever an object goes out of scope or is subjected to a delete

More specific questions:

1) If the object is created via a pointer and that pointer is later deleted or given a new address to point to, does the object that it was pointing to call its destructor (assuming nothing else is pointing to it)?

2) Following up on question 1, what defines when an object goes out of scope (not regarding to when an object leaves a given {block}). So, in other words, when is a destructor called on an object in a linked list?

3) Would you ever want to call a destructor manually?

  • 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-01T17:41:49+00:00Added an answer on June 1, 2026 at 5:41 pm

    1) If the object is created via a pointer and that pointer is later deleted or given a new address to point to, does the object that it was pointing to call its destructor (assuming nothing else is pointing to it)?

    It depends on the type of pointers. For example, smart pointers often delete their objects when they are deleted. Ordinary pointers do not. The same is true when a pointer is made to point to a different object. Some smart pointers will destroy the old object, or will destroy it if it has no more references. Ordinary pointers have no such smarts. They just hold an address and allow you to perform operations on the objects they point to by specifically doing so.

    2) Following up on question 1, what defines when an object goes out of scope (not regarding to when an object leaves a given {block}). So, in other words, when is a destructor called on an object in a linked list?

    That’s up to the implementation of the linked list. Typical collections destroy all their contained objects when they are destroyed.

    So, a linked list of pointers would typically destroy the pointers but not the objects they point to. (Which may be correct. They may be references by other pointers.) A linked list specifically designed to contain pointers, however, might delete the objects on its own destruction.

    A linked list of smart pointers could automatically delete the objects when the pointers are deleted, or do so if they had no more references. It’s all up to you to pick the pieces that do what you want.

    3) Would you ever want to call a destructor manually?

    Sure. One example would be if you want to replace an object with another object of the same type but don’t want to free memory just to allocate it again. You can destroy the old object in place and construct a new one in place. (However, generally this is a bad idea.)

    // pointer is destroyed because it goes out of scope,
    // but not the object it pointed to. memory leak
    if (1) {
     Foo *myfoo = new Foo("foo");
    }
    
    
    // pointer is destroyed because it goes out of scope,
    // object it points to is deleted. no memory leak
    if(1) {
     Foo *myfoo = new Foo("foo");
     delete myfoo;
    }
    
    // no memory leak, object goes out of scope
    if(1) {
     Foo myfoo("foo");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty basic programming question, I know PHP have a function for it, but does
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+
Very basic question - but I couldn't find an answer. I have got a
So I have been having a look at some of the magic that is
I have a very basic question about the LocalMon print monitor found at http://msdn.microsoft.com/en-us/library/windows/hardware/ff556478%28v=vs.85%29.aspx
I have a basic question regarding timers. My timer is acting very strange. I
I have a hobby project that is written in C# using MonoDevelop. I've been
I'm designing a link scraping program that grabs the basic link preview fields for
I have been experimenting with GNU Radio and came across the tunnel.py program. This
I am busy writing a program in Visual Basic .NET and I have a

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.