I know what a constructor does. It initializes a variable. However, what does that mean? What does it mean to initialize a variable? What is the purpose of doing that?
Thanks
Printable View
I know what a constructor does. It initializes a variable. However, what does that mean? What does it mean to initialize a variable? What is the purpose of doing that?
Thanks
No, it initializes an object, not a variable, and that's a big difference. It allocates memory for the object and all of its components, and any other housekeeping that needs be done including setting up any virtual method tables.
Small nitpick: a constructor doesn't allocate anything for the object to be itself; it's the new operator that does the dirty work, i.e. it allocates enough memory for the object; zeros all bits in the memory and plugs in a pointer to to the class function table; only then is the constructor called that further initializes the thing.
kind regards,
Jos