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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:23:00+00:00 2026-06-17T04:23:00+00:00

I have a question about a destructor’s implementation for a class. I understand the

  • 0

I have a question about a destructor’s implementation for a class. I understand the right way is using the ~ operator, but take a look at the following code:

class foo
{
private:
int* abc;

public:
foo()
{
abc = new int(2);
}

~foo()
{
delete abc;
}

void func()
{
delete abc;
}
}

Now let us say that the main function is defined as below:

int main(int argc, char** argv)
{
foo a;
a.func();
}

Upon the function call of func() in main, does this work in the exact same way as the destructor? What is the difference between the destructor and this function in any similar setting?

  • 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-17T04:23:01+00:00Added an answer on June 17, 2026 at 4:23 am

    func() and ~foo() do the exact same thing. And that’s the problem. When a goes out of scope, its destructor ~foo() will automatically be called, resulting in abc being deleted twice. One way to get around it would be to set abc to NULL at the end of func() after the delete, so that when the destructor gets called it deletes a NULL pointer, which is a special case in C++ where nothing is actually done and is a valid operation.

    Or, of course, the code could be rewritten in a way that actually made sense and accomplished something.

    And just to be really clear, the “difference” between func() and ~foo() is how/when they’re called, not in what they do. func() is manually called by the user, while ~foo() is automatically called when the variable goes out of scope. func() may be called zero or more times (it’s up to the programmer), but the compiler will call ~foo() exactly once (no more, no less) in this code.

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

Sidebar

Related Questions

I have a question about SqlDataReader: Is there some way to modificated the values
I have a question about the following code: #include <iostream> #include <boost/scoped_ptr.hpp> class Interface
I have a question about variable scope in a C++ class. The problem I'm
I have question about the need of using @configurable . I have gone through
I have one question about failed constructor and failed destructor in C++. I noticed
I have a question about the following code: class MyClass : private boost::noncopyable {
Good day everyone. I have a question about making and using derived classes of
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all

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.