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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:16:23+00:00 2026-06-09T13:16:23+00:00

I was experimenting with destructors in C++ with this piece of code: #include <iostream>

  • 0

I was experimenting with destructors in C++ with this piece of code:

#include <iostream>

struct temp
{
    ~temp() { std::cout << "Hello!" << std::endl; }
};

int main()
{
    temp t;
    t.~temp();
}

I see that “Hello!” is being printed twice. Shouldn’t the calling of the destructor free the object and the destructor shouldn’t be called again when it goes out of scope? Or is there some other concept?

(I do not intend to do this in practice. I’m just trying to understand what’s going on here.)

  • 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-09T13:16:24+00:00Added an answer on June 9, 2026 at 1:16 pm

    It happens because you told it to happen. The destructor for an automatic variable is always called when the variable goes out of scope. You also called it. That’s two calls total.

    Calling an object’s destructor does not signal to C++ not to call it again, since in normal execution there is no need to keep track.

    The solution is to never manually call your destructor.

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

Sidebar

Related Questions

While experimenting a bit with C++ templates I managed to produce this simple code,
I am experimenting with following code: http://www.jsresources.org/examples/MidiNote.java.html to operate my Novation Launchpad MIDI Controller.
Experimenting with Cocos2D collection detection and have some questions. First, some background: This is
Was experimenting with interface inheritance and found this: interface String{} interface Object{} interface Exception{}
In experimenting with this question I created an example that I utterly do not
In experimenting with pickle, I've put together some code for a (very) simple blog.
I started experimenting with SVG and I'm not sure if this is something I'm
Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
Experimenting using different approaches and reducing code maintenance I have ended up using reflection
While experimenting with this question on collections in Spring.NET , I discovered that Spring

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.