Menu

Opinions sought: constructors

2003-01-24
2012-09-26
  • Derek Baker

    Derek Baker - 2003-01-24

    Do real work in the constructor, or use an init member function?

    Derek

     
    • Anonymous

      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.

       
    • Derek Baker

      Derek Baker - 2003-01-24

      Thanks for the reply, Clifford.

      No-one else?

      Derek

       
    • Nobody/Anonymous

      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

       
    • Derek Baker

      Derek Baker - 2003-01-24

      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

       
      • Nobody/Anonymous

        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

         
    • Nobody/Anonymous

      init functions only make sense when applying the singleton pattern and the constructor is private for assuring a sole instance of the class

       
    • Nobody/Anonymous

      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

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.