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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:14:03+00:00 2026-05-18T10:14:03+00:00

I simply want a class that does this: class cleanup : boost::noncopyable { public:

  • 0

I simply want a class that does this:

class cleanup : boost::noncopyable
{
 public:
  typedef boost::function0<void> function;
  explicit cleanup( function f ) : func( f )
  {
  }

  ~cleanup()
  {
    func();
  }
  private:
    function func; 
};

The purpose being that I have this “cleanup” to invoke func when it is deleted. cleanup will be passed around as a shared_ptr.

I have considered also doing it using a custom deleter in boost::shared_ptr. Perhaps I could use boost::shared_ptr and just create with NULL and get func() to take a parameter that it ignores.

The purpose of this all is to abstract out a step I want the class’s destructor to perform when the last reference goes out of scope (actually remove itself from the container that holds it without the class having to know about its container).

If I do it with shared_ptr I would possibly pass around one of these:

boost::shared_ptr<void> cleanupObj( NULL, func );

Here the func must take a parameter. I am not 100% sure even that the deleter will always get called if the pointer is NULL so perhaps I need to use a different pointer which makes it start getting “messy” looking.

Is there a standard way to handle this and if not what is the best way?

  • 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-18T10:14:04+00:00Added an answer on May 18, 2026 at 10:14 am

    I believe that there is a Scope Exit library within Boost. However, why not just use the code you wrote in the OP? And, typically, if you have a class within a container, make the container’s owner dish out the references and handle what happens when there are none left.

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

Sidebar

Related Questions

I have a simple class that provides state codes like this: public class StateProvider
In .net, I have an inherited control: public CustomComboBox : ComboBox I simply want
I want to make a table that simply has two integer columns to serve
I'm writing a class that does essentially the same type of calculation for each
I want to keep the class simple and not defined a constructor so i
I simply want to call a swing action from my own popup menu. But
The subject says it all... I simply want Xcode to make a beep or
I've googled around and can't find a clear answer. I simply want to start
I'm struggling to think of a good way to use linq2sql, simply I want
I want to know how to simply publish over http = much like Mercurial

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.