Is it possible to overload a destructor




















Can global functions be overloaded? Which operator Cannot be overloaded with friend function? Can we overload destructor in Swift? You can't! Each class can only have one destructor.

What is the difference between method overloading and method overriding? Why sizeof is not in Java? What is sizeof in Java? Which method Cannot be overridden? Can we overload operator? Which of the following can be overloaded? Can a destructor return a value? Can destructors take arguments? Can constructor return a value? This website uses cookies to ensure you get the best experience on our website. More info. Overloading is also known as polymorphism.

Here a function or operator is seen in different forms or prototypes keeping the name as same. Overloading is often needed when functions or operators can take various parameters. Constructor and member functions are often overloaded to ease the design and interface. Compiler makes a default constructior and default destructor which does not take any argument default constructior or destructor are argument less.

That allows users to do lots of funky things. For example, if they have a chunk of the system that allocates memory like crazy then goes away, they could allocate all their memory from a Pool , then not even bother doing any delete s on the little pieces: just deallocate the entire pool at once. Another angle: many systems support a non-standard function often called alloca which allocates a block of memory from the stack rather than the heap. Naturally this block of memory automatically goes away when the function returns, eliminating the need for explicit delete s.

Assuming you survived the 6 or 8 lines of code needed to wrap your allocate function as a method of a Pool class, the next step is to change the syntax for allocating objects. The goal is to change from the rather clunky syntax new pool. To make this happen, you need to add the following two lines of code just below the definition of your Pool class:. Now when the compiler sees new pool Foo , it calls the above operator new and passes sizeof Foo and pool as parameters, and the only function that ends up using the funky pool.

Recall that the brute force approach sometimes used with placement new is to explicitly call the destructor then explicitly deallocate the memory:. Problem 1: plugging the memory leak. The actual code generated by the compiler is functionally similar to this:.

The point is that the compiler deallocates the memory if the ctor throws an exception. So the goal is to force the compiler to do something similar to what it does with the global new operator.

If it finds one, it does the equivalent of wrapping the ctor call in a try block as shown above. After this, the compiler will automatically wrap the ctor calls of your new expressions in a try block:. In other words, we add lines of code in one place your Pool header file and simplify an arbitrarily large number of other places every piece of code that uses your Pool class. For example, if!

Even though this technique requires a std::map look-up for each deallocation, it seems to have acceptable performance, at least in many cases. Joshua Joshua Since you can't, had you some idea how to use an overloaded destructor, or were you just curious? If you want to do something other than just make two destructors for one class, bring it up and we can discuss how to accomplish it. I wonder if it was possible, how you'd expect it to behave? It was a question brought up by someone else.

They wanted to see just how fast SO would respond. It proved the point. They are very excited about SO now. I went a-Googling for this exact question. Add a comment. Active Oldest Votes. You can't. Improve this answer. JaredPar JaredPar k gold badges silver badges bronze badges.

Qberticus, yes it would in some ways. Without more info from the OP I can't tell. I'm hoping the OP considered this and is taking that into account in their solution. Dima Dima



0コメント

  • 1000 / 1000