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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:17+00:00 2026-05-28T11:09:17+00:00

See this code here: class test { int n; int *j; public: test(int m)

  • 0

See this code here:

class test
{
    int n;
    int *j;
public:
    test(int m)
    {
        n = 12;
        j = new int; 
        cin >> *j;
    }

    void show()
    {
        cout << n << ' ' << *j << endl;
    }

    ~test()
    {
        delete j;
    }

};

int main()
{
    test var = 123;
    var.show();
    return 0;
}

In this program compiler should complain about double deleting of j. First delete is done when temporary object temporary(123) is destroyed. The second delete is done when var object is destroyed. But this is working fine?

Does it mean temporary object does not call destructor?

  • 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-05-28T11:09:17+00:00Added an answer on May 28, 2026 at 11:09 am

    The contentious line is this:

    test var = 123;
    

    The relevant standard text (that the pundits in the comments are referencing), I believe, is (8.5, “Declarators”):

    The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. The temporary is an rvalue. The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized;

    Indeed, in 12.6, we get an example of this:

    complex f = 3;  // construct complex(3) using
                    // complex(double)
                    // copy it into f
    

    Thus, in your use of =, your implementation is probably directly constructing object and eliminating the intermediate temporary entirely (and, as the comments have noted, most do).

    This class doesn’t copy properly, so creating a copy of it (and the freeing the copy and the original) would result in a double delete (and crashes, undefined behavior, etc.). Because no copies are created, this scenario does not happen above.

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

Sidebar

Related Questions

Here is the test code template <class T> void f() { T t; t.f<T>(0);
I have the following: <p>This is a test</p> <pre>public class Car { protected Car()
I have a Activity entity, this entity can have several Task. Here the code
I have a class definition that looks like this: public class SolrObj { [SolrUniqueKey(id)]
Here is the C# code I am trying to translate: public bool equals(Matrix matrix,
I'm using the source code found on this site here: http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/comment-page-1/#comment-2562 A fantastic resource,
I have code of the following form: class Test { private final A t;
JAVA Code Here is some part of my code that I have written in
I wrote a small queue class in C++, on Windows. Just to test, I'm
Here I create a class in JAVA in which I have function (callback) which

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.