Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Destructor syntax  





2 In C++  



2.1  Example  







3 In C with GCC extensions  





4 Xojo  





5 See also  





6 References  














Destructor (computer programming)






Čeština
Español
فارسی
Français

Italiano
Lietuvių
Magyar
Македонски
Nederlands

Polski
Português
Русский
Shqip
Slovenščina
Svenska
Українська
Tiếng Vit


 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Inobject-oriented programming, a destructor (sometimes abbreviated dtor[1]) is a method which is invoked mechanically just before the memory of the object is released.[2] It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources (memory allocations, open files or sockets, database connections, resource locks, etc.) which were acquired by the object during its life and/or deregister from other entities which may keep references to it. Use of destructors is needed for the process of Resource Acquisition Is Initialization (RAII).

With most kinds of automatic garbage collection algorithms, the releasing of memory may happen a long time after the object becomes unreachable, making destructors (called finalizers in this case) unsuitable for most purposes. In such languages, the freeing of resources is done either through a lexical construct (such as try..finally, Python's "with" or Java's "try-with-resources"), which is the equivalent to RAII, or explicitly by calling a function (equivalent to explicit deletion); in particular, many object-oriented languages use the Dispose pattern.

Destructor syntax[edit]

In C++[edit]

The destructor has the same name as the class, but with a tilde (~) before it.[2] For example, a class called foo will have the destructor ~foo(). Additionally, destructors have neither parameters nor return types.[2] As stated above, a destructor for an object is called whenever the object's lifetime ends.[2] If the object was created as an automatic variable, its lifetime ends and the destructor is called automatically when the object goes out of scope. Because C++ does not have garbage collection, if the object was created with a new statement (dynamically on the heap), then its destructor is called when the delete operator is applied to a pointer to the object. Usually that operation occurs within another destructor, typically the destructor of a smart pointer object.

In inheritance hierarchies, the declaration of a virtual destructor in the base class ensures that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class. Objects that may be deleted in this way need to inherit a virtual destructor.

A destructor should never throw an exception.[7]

Non-class scalar types have what's called a pseudo-destructor which can be accessed by using typedef or template arguments. This construct makes it possible to write code without having to know if a destructor exists for a given type.

int f() {
    int a = 123;
    using T = int;
    a.~T();
    return a; // undefined behavior
}

In older versions of the standard, pseudo-destructors were specified to have no effect, however that was changed in a defect report to make them end the lifetime of the object they are called on.[8]

Example[edit]

#include <cstring>
#include <iostream>

class Foo {
public:
    Foo(): data_(new char[sizeof("Hello, World!")]) {
        std::strcpy(data_, "Hello, World!");
    }

    Foo(const Foo& other) = delete;             // disable copy construction
    Foo& operator=(const Foo& other) = delete;  // disable assignment

    ~Foo(void) { delete[] data_; }

private:
    friend std::ostream& operator<<(std::ostream& os, const Foo& foo) {
        os << foo.data_;
        return os;
    }

    char* data_;
};

int main() {
    Foo foo;
    std::cout << foo << std::endl;
}

Objects which cannot be safely copied and/or assigned should be disabled from such semantics by declaring their corresponding functions as deleted within a public encapsulation level. A detailed description of this method can be found in Scott Meyers' popular book, Effective Modern C++ (Item 11: "Prefer deleted functions to private undefined ones."[9]).

UML class in C# containing a constructor and a destructor.

In C with GCC extensions[edit]

The GNU Compiler Collection's C compiler comes with 2 extensions that allow implementing destructors:

Xojo[edit]

Destructors in Xojo (REALbasic) can be in one of two forms. Each form uses a regular method declaration with a special name (with no parameters and no return value). The older form uses the same name as the Class with a ~ (tilde) prefix. The newer form uses the name Destructor. The newer form is preferred because it makes refactoring the class easier.

Class Foobar
  // Old form
  Sub ~Foobar()
  End Sub

  // New form
  Sub Destructor()
  End Sub
End Class

See also[edit]

References[edit]

  1. ^ "dtor". TheFreeDictionary.com. Retrieved 2018-10-14.
  • ^ a b c d e Sebesta, Robert W. (2012). ""11.4.2.3 Constructors and Destructors"". Concepts of Programming Languages (print) (10th ed.). Boston, MA, USA: Addison-Wesley. p. 487. ISBN 978-0-13-139531-2.
  • ^ Constructors and Destructors, from PHP online documentation
  • ^ "3. Data model — Python 2.7.18 documentation".
  • ^ "3. Data model — Python 3.10.4 documentation".
  • ^ "Destructors - the Rust Reference".
  • ^ GotW #47: Uncaught exceptions Accessed 31 July 2011.
  • ^ Smith, Richard; Voutilainen, Ville. "P0593R6:Implicit creation of objects for low-level object manipulation". open-std.org. Retrieved 2022-11-25.
  • ^ Scott Meyers: Effective Modern C++, O'REILLY, ISBN 9781491903995
  • ^ C "destructor" function attribute
  • ^ Erickson, Jon (2008). Hacking the art of exploitation. No Starch Press. ISBN 978-1-59327-144-2.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Destructor_(computer_programming)&oldid=1231202524"

    Categories: 
    Method (computer programming)
    C++
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
    Articles with example C++ code
     



    This page was last edited on 27 June 2024, at 01:32 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki