Thread: OO and U
View Single Post
Old 05-11-2010, 08:22 AM   #15
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by FanIn80 View Post
Sorry, one last question if you guys don't mind... How does a "constructor" fit into all of this? Is it just where you define how an object is instantiated?

There's also a copy constructor, which is an important concept. It allows you to define the behaviour when your object is copied. For instance, if you have a class containing pointers as member variables, you likely don't want to copy the pointers into the new object. Otherwise, you'd have two pointers pointing to the same memory address and thus modifying one would modify the other, which is most cases is not what you want. You can define a copy constructor to specify exactly what is done with the member variable when copying an object.

It's one of those things you have to be careful with when working in language that allow you to control memory (like C++). If you're not, it can lead to loads of nasty bugs.

It typically looks something like this:

myObject( const &myObject copyObject);

The const is highly advisable so you don't accidentally modify the source object.

Edit: Sorry, I realize this was a bit beyond the scope of your original question. You may not have deleved into the topic of pointers. Suffice it to say you can use a copy constructor to define how exactly one object is copied from another.

Last edited by BlackEleven; 05-11-2010 at 08:52 AM.
BlackEleven is offline   Reply With Quote