Im having trouble, what exactly is a constructor?
Im having trouble, what exactly is a constructor?
a function that get's called on creation of an object. It's what you might have done with InitStruct(&thestruct); in C but it get's called automatically
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
A constructor is nothing compared to a destructor.
But seriously...a constructor just initializes all your member fields to an initial value. Trust me, you'll come across this situation sooner or later, then you'll say "Ah. That's what its for."
It's a guy in a hardhat that eats from a lunch truck..
A constructor is a public (normally) function that belongs to a class. When a new object is created, the appropriate constructor (there can be more than one) is automatically called. They are usually used to initialize member variables, call parent class constructors, and really just class initialization in general.
(I know you didn't ask, but) A deconstructor is similar to a constructor. There may only be one deconstructor in a class. It too is called automatically, but when an object goes out of scope and is used to deinitialize an object (ie: deleting allocated memory, freeing resources, etc).