Constructors and Destructors
1.) What exactly do they -- DO --?
and
2.) Can I get by without them??
Why don''''t we just kill the guys who write error messages. It''''d make our lives a hell of a lot easier
****************************
Gimme a break, I''''m Just 13 (14 in JULY)
1) If you have a variable of a given class, when the variable is created, the constructor is called. When the variable is copied, the copy-constructor is called. When the variable is destroyed, the destructor is called. What do they do ? Well, anything, it depends on the class. The constructor does initialisation (e.g. open a file given its name, create a list), the destructor does cleanup (e.g. closing files, deleting the list elements)
2) You could, the compiler automatically generates a default constructor that does... nothing, if you don''t specify one of your own. But they are a fundamental C++ concept, so you are only restricting yourself to the most basic types. Especially given the fact that many classes in the standard library do have constructor and destructor, so you need to understand the concept if you really want to use them.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
2) You could, the compiler automatically generates a default constructor that does... nothing, if you don''t specify one of your own. But they are a fundamental C++ concept, so you are only restricting yourself to the most basic types. Especially given the fact that many classes in the standard library do have constructor and destructor, so you need to understand the concept if you really want to use them.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
1) Fruny already did a good job explaining this one.
2) Just to expand on what Fruny said, constructors and destructors come in really handy if your class contains any dynamically allocated data (especially if you''re forgetful ). Say, for example, you had a class with a dynamically allocated linked list as a member. Before you could use that list, you''d have to allocate space for it and initialize it. A constructor is an excellent place to put this code. When a object of that class is instantiaed (either dynamically or if you declare it statically) your constructor is automagically called and your list would be allocated/initialized.
Now to prevent memory leaks you need to deallocate your list when you''re through with that object. The destructor is the perfect place for this kind of code. When your object falls out of scope or you (hopefully don''t forget to ) delete its pointer, your destructor is called for you and your list is deallocated.
One note, it''s generally not a good idea to call the constructor and destructor yourself unless you know what you''re doing and you have a good reason too (one of which escapes me right now).
Just remember this mantra: "Construtors and destructors are good..." :D
/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
2) Just to expand on what Fruny said, constructors and destructors come in really handy if your class contains any dynamically allocated data (especially if you''re forgetful ). Say, for example, you had a class with a dynamically allocated linked list as a member. Before you could use that list, you''d have to allocate space for it and initialize it. A constructor is an excellent place to put this code. When a object of that class is instantiaed (either dynamically or if you declare it statically) your constructor is automagically called and your list would be allocated/initialized.
Now to prevent memory leaks you need to deallocate your list when you''re through with that object. The destructor is the perfect place for this kind of code. When your object falls out of scope or you (hopefully don''t forget to ) delete its pointer, your destructor is called for you and your list is deallocated.
One note, it''s generally not a good idea to call the constructor and destructor yourself unless you know what you''re doing and you have a good reason too (one of which escapes me right now).
Just remember this mantra: "Construtors and destructors are good..." :D
/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement