c++ - Default Parameters versus In-Class Member Initialization versus Delegating Constructors -


i make decision method should use construct complex objects multiple constructors.

so pros , cons of default parameters, in-class member initialization , delegating constructors wrt each others?

default parameters

class defaultparams { public:   defaultparams(int = 42,                 const std::string &b = "42")                 :                 m_a(a),                 m_b(b)   {     // things   };    defaultparams(const std::string &b)     :     m_a(42),     m_b(b)   {     // things   };  private:   int m_a;   std::string m_b; }; 

pros: less constructors; cons: repeat code or need init() method.

in-class initialization

class inclass { public:   inclass()   {     // things   };    inclass(int a)     :      m_a(a)   {     // things   };    inclass(const std::string &b)     :     m_b(b)   {     // things   };    inclass(int a,           const std::string &b)     :     m_a(a),     m_b(b)   {     // things   };  private:   int m_a = 42;   std::string m_b = "42"; }; 

pros: ? cons: more contructors, repeat code or need init() method.

delegating constructors

class delegatingctor { public:   delegatingctor()     :     delegatingctor(42, "42")   {   };    delegatingctor(int a)     :     delegatingctor(a, "42")   {   };    delegatingctor(const std::string &b)     :     delegatingctor(42, b)   {   };    delegatingctor(int a, const std::string &b)     :     m_a(a),     m_b(b)   {     // things   };  private:   int m_a;   std::string m_b; }; 

pros: not repeat code; cons: more constructors.

of course, there lot more. help.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -