What is a constructor? And what is the 'this' pointer. Thanks guys.
-CDudd
What is a constructor? And what is the 'this' pointer. Thanks guys.
-CDudd
A constructor is called whenever an instance of a class is created on the stack or heap (as long as the memory is allocated with new keyword), to perform initialisation.
The 'this' pointer points to the current instance of a class from within a class, and is inserted by the compiler as a hidden argument for each class member function call.
zen
So the constructor is an initializer for what? The class it is within?
Thanks
-CDudd
So the constructor is an initializer for what? The class it is within?
First, you need a class. If you don't know what that is, look it up. You use pre-existing ones or define your own.
A constructor initializes an object of the class to default values when it's declared. Without one, the object could have incorrect values.