9/22/2015
C++ is not a managed language. This basically means that there is no garbage collector. The good thing about this is that there is no startup time or garbage collector overhead. The design goal of C++ was to be:
This means that they make speedups that may not seem right until you understand the reasoning behind it. But this post is about destructors, arguably the best feature of C++ that doesn't exist in Java. Destructors force the destruction of resources when a block of code ends. Let's look at an example:
#include <fstream> #include <iostream> #include <string> int main() { using namespace std; ifstream file_stream("file"); string first_line; getline(file_stream, first_line); cout << "First line of file is: " << first_line << endl; }
The first two lines tell the compiler to basically import the file stream and input output stream headers (fstream and iostream respectively). The <> around the name tells it that it is a system header and that you didn't make it.
The line using namespace std;
tells the
compiler to look up any members it doesn't see itself in the
namespace std
. For example, this shortens the
usage of std::cout
to cout
.
The ifstream
allow you to read input from a
file, assuming that it exists. We will for the sake of the
example. In Java you would have to wrap this operation in a
try catch block, then close the resource at the end. If an
exception occurs that you don't catch, the file will never
be closed! In C++ the destructor for an object will always
be called when the resource falls out of scope: in this case
at the end of the main function. This makes sure that
resources are properly disposed no matter what,
removing the resource cleanup strain from the user, the
point of a garbage collector in the first place!
The line string first_line;
allocates a
string that will be deleted when the main function ends.
Writing String firstLine;
in Java will
allocate a pointer to a String but won't actually
allocate it. You can do the exact same thing, but it is
bad form. It is very similar to a String
in
Java except that it is modifiable. You may think that
immutable Strings in Java are a great feature and they
are, but C++ allows you a variable as immutable by adding
the keyword const
rather than designing two
different classes to do the same thing. This has a great
speed improvement as you don't have central cache of
immutable strings and don't need multiple allocations for
one string object.
The last line of real code is a function call
of getline
. This reads a line out of a stream
and puts it into the second variable given, a string
reference. A reference simply means that that other method
can change the contents of it, just like a normal Object in
Java.
The final line just prints out the first line of the file
that you acquired in the getline()
statement.
All the destructors are actually called at the very last
line, the }
. First the string
first_line
will be deleted, then
the ifstream
. This ensures that the
resources acquired are deleted no matter what happens
after they are aquired: a process called RAII - Resource
Acquisition Is Initialization.