How much to do in a contructor

This is a discussion on How much to do in a contructor within the C++ Programming forums, part of the General Programming Boards category; I would also say that multistage construction is not a good practice, unless an object can be reused -- that ...

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I would also say that multistage construction is not a good practice, unless an object can be reused -- that is if it it makes sense to redo the second construction step for the object. For example, if you want you socket to be re-binable to a different port, it makes sense to have a constructor that does not bind the port, and have a function that binds it later. It is still not a bad idea to have a constructor that does take a port. This is similar to the behavior of std::fstream.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    This is similar to the behavior of std::fstream.
    std::fstream supports both approaches. You can pass the filename to the constructor or you can just create the object and then call open. If you just create the object and never call open the stream is not ready to be used. But if you pass the filename into the constructor and then call open that is just redundant code. Objects that support both types of creation can become confusing but again it is very minimal confusion.

    I've seen objects that support one or the other and/or both and as long as the user is clear on how to use the object it usually does not cause any problems. I'm not endorsing one or the other here.

  3. #18
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Wrapping a declaration in a try block obviously won't work, because the variable will go out of scope immediately. It is better to catch the exception at a higher level. Or, put the entire function body inside the try block.

    Although it's an unusual practice, in some environments the decision has been made that exceptions will not be used -- in such cases, there is really no good alternative to two-phase initialization.

    In this particular case, I think the creation/binding/listening of the socket in the constructor is the appropriate method. This is RAII. If the object was constructed, then everything is as expected and the object can be used normally. If any one of those calls fails, the object is inherently unusable and an exception should be thrown.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling the contructor again
    By symbiote in forum C++ Programming
    Replies: 10
    Last Post: 05-09-2009, 09:54 AM
  2. a data member that has no default contructor?
    By symbiote in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2009, 04:06 AM
  3. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 08:43 PM
  4. Quick Question regarding Contructor Variables
    By cram in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2004, 08:29 PM
  5. contructor not allocating array properly?
    By BrianK in forum Windows Programming
    Replies: 4
    Last Post: 07-13-2004, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21