2

My class has many constructors and it has a lot to do in the constructor so I was thinking of making a private construct() function to promote code reuse. Is this a good idea? Because I see many libraries that do not do this and have code duplication.

Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 3
    Be careful when calling virtual functions from the constructor, though. It might not behave as you would expect; the version called will be the one that belongs to the class whose constructor is currently being invoked, not the one belonging to what's actually being created. – Dawson Jan 10 '11 at 23:54
  • What is the reason that you have many constructors? What are the differences between the constructors? – Cheers and hth. - Alf Jan 10 '11 at 23:56
  • @Alf P. Steinbach They are very small, things like a string, just things to add convenience – jmasterx Jan 11 '11 at 00:01
  • @Toolbox yea I'm aware of that, thanks though :) – jmasterx Jan 11 '11 at 00:04
  • well concrete examples would help us help you, but from what you say I think that most of it can be handled by default arguments, formal argument types that do relevant conversions, and/or the Named Parameters Idiom (see the FAQ). NPI in short is to pass a general options object with chainable setters, one setter per options, like `MyFileClass f( FileOptions().name( "blah" ).locked( false ).createIfNeeded( true ) );` Cheers & hth., – Cheers and hth. - Alf Jan 11 '11 at 00:06

3 Answers3

3

This is totally fine and is probably a good idea. What might be concerning is having so many constructors. But it's impossible to say.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
1

Take a look at this question, which addresses the issue in a general way.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
1

As long as construct isn't virtual, it's generally safe. I tend to prefer going the other direction though.

Make as few general constructors as possible, and if I need a lot of easier-to-use convenience construction functions, I use free (non-member) functions, which call the constructor, filling in the missing parameters with suitable default values, and then return the constructed object.

That way I avoid the somewhat iffy issue of constructors calling member functions of the not-yet-constructed object, and I avoid duplicating code in the class.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • If the convenience functions are static members, they can also access the guts of the object and do initialization beyond what the constructor allows. – Nate Jan 11 '11 at 00:19
  • Yes, but what's the point? If you need to do *more* than what the constructor allows, I'd say it reeks of bad design. And why drag more code into the class? I prefer manageable classes that focus on one responsibility. And 14 different convenience functions for instantiating the class typically water down that responsibility, and add *a lot* of clutter if they're defined in the class. – jalf Jan 11 '11 at 01:44