Constructors and Destructors


Home

About the Author

General Programming

Pascal

Delphi

C&C++

Visual Basic

SQL

JAVA Script

Links

 

Assert vs Verify ] [ Constructors and Destructors ] Falling through switch statements ] Memory Leaks ] Null Terminator ] Rogue array pointers ]

This isn't necessarily a pitfall, but it is certainly worth mentioning that although class (and structure) constructors and destructors are functions, they are quite different from any other function and you should know the difference. Here is a list of interesting points about these two special types of functions.

1. Neither constructors nor destructors have return types (not even void).

2. A destructor cannot take arguments (not even void).

3. A destructor cannot be overloaded.

4. Both constructors and destructors are called implicitly, so you never need to call them yourself.

5. Constructors cannot be declared virtual.

6. Both constructors and destructors must have the same name as its class (structure), except a destructor has a tilde (~) in front of it.

7. Even if you don't explicitly write a constructor and destructor for a class, the compiler will write default ones for you (because a class must have both).

Link of the week 
http://www.cpp-
programming.
com/
Newsgroups
comp.lang.c
comp.lang.c++
comp.programming

This page is best viewed in 800x600x256. This page was last edited Saturday, July 15, 2000