Are constructors basically there to initialize data members in a class?
Are constructors basically there to initialize data members in a class?
I would say that most constructors call the constructors of its members and does nothing else. Really it depends on the object and how it is used; there may be occasions where you think you have to do something in addition to that, like write in a log, or send a message to the winproc in a WinAPI program, or something else. You always want the object to be ready to do what it is supposed to do after it is finished being initialized, and that can mean a lot of things.
Last edited by whiteflags; 08-31-2014 at 07:35 PM.
Stroustrup puts it this way: a constructor is "used to initialize objects of its class. Often used to establish an invariant for the class. Often used to acquire resources. A constructor establishes a local environment in which member functions execute."Originally Posted by jaxdid
So, as part of the process of initialising an object of the class, the constructor would typically initialise the data members, but the scope of what initialising an object of the class means can go beyond that.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)