Lattice C++
03-Feb-90 18:30:37
Sb: #88399-Lattice C++
Fm: Thomas Holaday 70407,534
To: Steve Ahlstrom 76703,2006
Simple case:
class Window
{
private:
char *name;
short xpos, ypos, xwid, ywid ;
…
public:
Window() ; Window(char *s); ~Window();
void MoveWindow(short newx, newy);
}
Window::Window() {
if (necessary-libs-not-open)
open_the_necessary_libs;
xpos = ypos = 0; name = "Default Window" ;
xwid = ywid = 50;
}
Window::Window(char *s) {
xpos = ypos = 0; name = new char [strlen(s)+1]; strcpy(name, s);
xwid = ywid = 50;
}
Window::~Window() {
delete name; if (I am the last Window)
close_the_libraries_and_release_the_memory;
}
…
main()
{
Window a;
}
Simply declaring variable a to be of type Window causes the compiler to do
everything you specified in your "constructor." In this case, two different
constructors were defined, one for default windows, one for named windows.
The compiler sees "Window a" and uses the default constructor. If you had
written "Window a("Showing Off")" you'd get the named constructor. Once
the variable passes out of scope, the compiler calls all the cleanup code
in your "destructor," named by convention as ~<class being destroyed>. In
complex environments such as Intuition where there's a lot of preliminary and
a lot of cleanup, this is a keystroke-saver. By the way, there are at least
a half dozen bugs in the example code, but you get the idea.