Do real work in the constructor, or use an init member function?
Derek
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-01-24
If the class implements an abstract data type, it should behave just like a concrete data type, and therefore everything required to make it valid and operational should be done in the constructor.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
i think main work should be done in the constructor as well... for a different reason than cliff... er clifford... making a user do something goofy like:
DerekClass dcclass;
dcclass.init();
*OR*
DerekClass dcclass;
dcclass.init("something that MUST be sent from the user");
if you do not need to know user assigned data there is absolutely NO reason NOT to do the work in the constructer... that is pritty much why they are there... if you need complex data from the user... you might just want to build multiple versions to handle incase the user doesnt want to send the data when it is created...
Zero Valintine
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
the interface should be separated, and the implementation access be that it's structured so it can be ignored by a user, and at the same time the job to initialize be that of the user through the interface.
A user's problem may require some complexity, complexity that you the class designer cannot have any idea about, other then the knowledage of the functionality you the designer have in the class's implementation. Constructors are necessary, but are of little use to a user if unable to have some control of them at that the user's level.
`l
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Do real work in the constructor, or use an init member function?
Derek
If the class implements an abstract data type, it should behave just like a concrete data type, and therefore everything required to make it valid and operational should be done in the constructor.
Thanks for the reply, Clifford.
No-one else?
Derek
i think main work should be done in the constructor as well... for a different reason than cliff... er clifford... making a user do something goofy like:
DerekClass dcclass;
dcclass.init();
*OR*
DerekClass dcclass;
dcclass.init("something that MUST be sent from the user");
if you do not need to know user assigned data there is absolutely NO reason NOT to do the work in the constructer... that is pritty much why they are there... if you need complex data from the user... you might just want to build multiple versions to handle incase the user doesnt want to send the data when it is created...
Zero Valintine
Thanks for the reply Zero.
In this case the class is for me, not other users, but I'm inclining toward using the constructor.
Derek
well... keep that reference handy in case you ever make a good wrapper you want to use... and that maybe others would find useful
Zero Valintine
init functions only make sense when applying the singleton pattern and the constructor is private for assuring a sole instance of the class
Derek
the interface should be separated, and the implementation access be that it's structured so it can be ignored by a user, and at the same time the job to initialize be that of the user through the interface.
A user's problem may require some complexity, complexity that you the class designer cannot have any idea about, other then the knowledage of the functionality you the designer have in the class's implementation. Constructors are necessary, but are of little use to a user if unable to have some control of them at that the user's level.
`l