Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Destructor methods in C++ - a primer

Constructor methods - named pieces of code which create a new object - are key to any object oriented program / programming. Without objects, an object oriented program isn't really an object oriented program, and they need to be created somehow. But what happens at the other end of the life cycle?

When a program completes, it's flushed from memory and that means that the obejcts that have been used are 'lost'. In most cases, by this time they have fulfilled theie purpose and can simply be allowed to disappear - but that's not always the case; work in progress may be held within an object which needs saving to permanent storage, for example. And what if you want to get rid of an object earlier - after it's fulfilled its purpose, so that you can reuse the memory it has occupied.

In order to meet these extra requirements, object oriented languages support destructor methods. How they're implemented, and how and when they're run differ from language to language ... I'm going to use C++ as am example, using an example [here] which I wrote during yesterday's C++ for C Programmers course.

How is a constructor declared and defined?

It's declared as a method with the same name as the name of the class with a leading tilde, and without any parameters:
  class Book {

(example also shows a constructor with a single parameter)

What is does is defined as a method in the usual way. Here's an example of that destructor's code with - for learning purposes - a message output to tell you that it's being run:
  Book::~Book() {cout << "Gone " << npages <

When do destructors in C++ run?

1. For objects created on the stack - for example
  Book pottering(156);
the destructor is run when the variable goes out of scope - i.e. at the end of execution of the block in which it is defined. So if it's in main that will be when the program terminates, and if it's in another method that will be much earlier and indeed objects may be lost and recreated many times over in a functions that's called repeatedly

2. For objects created on the heap - for example
  Book * inbed = new Book(99);
you'll call the destructor directly when you're done with them - e.g.
and failure to do so can in some circumstances lead to an application which grows, grabbing more and more memory. For a short-run program that may not be an issue, but for a server process that can be whet'a often described as a "memory leak". Particular care need to be taken of apparently inocuous code lines such as
  inbed = new Book(45);
which loose the memory held by (any) previous Book object creation unless a second copy of the pointer has been stored so that you can retain access. This is rather different to other OO Languages which retain link counts for objects and automatically schedule the destructor to be run when the link count is reduced to 0
(written 2011-11-05)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C233 - C and C based languages - OO in C++ - beyond the basics
  [798] References and Pointers in C++ - (2006-07-10)
  [801] Simple polymorphism example - C++ - (2006-07-14)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1572] C - structs and unions, C++ classes and polymorphism - (2008-03-13)
  [1674] What a lot of files! (C++ / Polymorphism demo) - (2008-06-12)
  [1819] Calling base class constructors - (2008-10-03)
  [2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
  [2845] Objects and Inheritance in C++ - an easy start - (2010-07-01)
  [3056] C++ - a complete example with polymorphism, and how to split it into project files - (2010-11-16)
  [3123] C++ objects - some short, single file demonstrations - (2011-01-07)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3142] Private and Public - and things between - (2011-01-22)
  [3244] C and C++ - preprocess, compile, load, run - what each step is for - (2011-04-12)
  [3251] C++ - objects that are based on other objects, saving coding and adding robustness - (2011-04-17)
  [3811] Associated Classes - using objects of one class within another - (2012-07-21)
  [3979] Extended and Associated objects - what is the difference - C++ example - (2013-01-18)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4375] Final examples for 2014 - and a look at our 2015 training course options - (2014-12-31)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4560] Variables, Pointers and References - C and C++ - (2015-10-29)

Back to
Melksham Forward - meeting summary and pictures
Previous and next
Horse's mouth home
Forward to
Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses
Some other Articles
A colleague and friend who changed my life
Melksham has a heart - have your picture taken and support the defibrillator appeal
The skull above the door in Melksham Town Hall
Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses
Destructor methods in C++ - a primer
Melksham Forward - meeting summary and pictures
Cold call contacts - preference services and turning off spam sales approaches
Well House Manor - Winter Warmers - Open 7 days a week, 11 to 5 ++
Coaches from Trowbridge and Melksham to London
Melksham House - a community facility
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3508_Des ... rimer.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb