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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:38:02+00:00 2026-06-04T17:38:02+00:00

Possible Duplicates: C++: Delete this? Object-Oriented Suicide or delete this; I’m learning C++ by

  • 0

Possible Duplicates:
C++: Delete this?
Object-Oriented Suicide or delete this;

I’m learning C++ by reading the very good book C++ Primer and I’m learning how C++ deallocates memory by the delete keyword like C does with free. Java and Pascal do not have this mechanism for explcitly freeing memory. It could lead to errors in programs if they run for long time and variables are destroyed that are needed so it should not be trivialized.

In short I wonder if it’s legal or advicable for instance in C++ for a variable to do this.delete() and delete itself. We mostly hear about freeing pointers in C and C++ and this is done with new free and delete keywords. Pascal also has pointers but Java has not. So in Java it should not be possible since you do not explicitly delete objects, C doesn’t have objects so a struct couldn’t free the memory it was allocated even if it was technically possible since C does not have objects and neither does Pascal.

So I suppose that leaves C++ for my question whether it is legal for an object to delete itself with something like this.delete()?

  • 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-04T17:38:04+00:00Added an answer on June 4, 2026 at 5:38 pm

    It’s perfectly possible for an object to do delete this;.

    However, after doing so, using thisis an undefined behaviour.

    So, if you are very careful with was is done afterward, it’s fine and legal for an object to “commit suicide” by doing delete this;

    But, It’s really not a good idea, especially because it means that your class should only be instanciated by new, as an allocation on te stack could cause the destructor to be called twice: by the delete this, and when going out of context.

    The following example shows why it’s not a good idea:

    class A
    {
    public:
        ~A() { std::cout << "Destructor"; }
        void foo() { delete this; } 
    };
    
    int main()
    {
        {
            A a;
            a.foo(); // OK, a is deleted
        } // Going out of context, the destructor is called again => undefined behavior
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicates: Is it OK to use "delete this" to delete the current object?
Possible Duplicate: Is it OK to use “delete this” to delete the current object?
Possible Duplicate: PHP detecting request type (GET, POST, PUT or DELETE) This should be
Possible Duplicates: What does the PHP syntax $var1->$var2 mean? Reference - What does this
Possible Duplicates: Why is there a special new and delete for arrays? ( POD
Possible Duplicate: What is difference between instantiating an object using new vs. without This
Possible Duplicate: this.href vs $(this).attr('href') Here is my code: $(function () { $(a.Delete).click(function ()
Possible Duplicate: Can you write object oriented code in C? Hi! Just for the
Possible Duplicates: How does delete[] “know” the size of the operand array? ( POD
Possible Duplicate: C++ delete - It deletes my objects but I can still access

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.