Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualServant of the Lord

Posted 03 October 2012 - 09:00 PM

Not just references, but also inherited classes must be constructed in initializer lists:
SubClass::SubClass(int blah) : BaseClass(blah) { }

With C++11 you can use "constructor delegating" (to call one constructor from another constructor) - this must also be done by initializer lists:
class MyClass()
{
	 public:
	 MyClass() : MyClass("Unnamed", 5ft + 9in, BrownColor, 160lbs) {}
	 MyClass(std::string name, Color eyeColor) : MyClass(name, 5ft + 9in, eyeColor, 160 lbs)  {}
	 MyClass(std::string name, int height, int weight) : MyClass(name, height, BrownColor, weight) {}
	 MyClass(std::string name, int height, Color eyeColor, int weight) : name(name), height(height), eyeColor(eyeColor), weight(weight) {}
}
The above is C++11 specific code.

How-so ever! Also in C++11, you get 'Non-static data member initializers'. *squeals with joy*

This will allow you to do this instead:
class MyClass()
{
	 public:
	 MyClass() :  {}
	 MyClass(std::string name, Color eyeColor) : name(name), eyeColor(eyeColor)  {}
	 MyClass(std::string name, int height, int weight) : name(name), height(height), weight(weight) {}
	 MyClass(std::string name, int height, Color eyeColor, int weight) : name(name), height(height), eyeColor(eyeColor), weight(weight) {}

	 private:
	 std::string name = "Unnamed"; //I get to do default initialization right in the class body! Sweet!
	 int height = 5ft + 9in;
	 int weight = 160lbs;
	 Color eyeColor = BrownColor;
}

#1Servant of the Lord

Posted 03 October 2012 - 08:59 PM

Not just references, but also inherited classes must be constructed in initializer lists:
SubClass::SubClass(int blah) : BaseClass(blah) { }

With C++11 you can use "constructor delegating" (to call one constructor from another constructor) - this must also be done by initializer lists:
class MyClass()
{
     public:
     MyClass() : MyClass("Unnamed", 5' 9", BrownColor, 160lbs) {}
     MyClass(std::string name, Color eyeColor) : MyClass(name, 5' 9", eyeColor, 160 lbs)  {}
     MyClass(std::string name, int height, int weight) : MyClass(name, height, BrownColor, weight) {}
     MyClass(std::string name, int height, Color eyeColor, int weight) : name(name), height(height), eyeColor(eyeColor), weight(weight) {}
}
The above is C++11 specific code.

How-so ever! Also in C++11, you get 'Non-static data member initializers'. *squeals with joy*

This will allow you to do this instead:
class MyClass()
{
	 public:
	 MyClass() :  {}
	 MyClass(std::string name, Color eyeColor) : name(name), eyeColor(eyeColor)  {}
	 MyClass(std::string name, int height, int weight) : name(name), height(height), weight(weight) {}
	 MyClass(std::string name, int height, Color eyeColor, int weight) : name(name), height(height), eyeColor(eyeColor), weight(weight) {}

     private:
     std::string name = "Unnamed"; //I get to do default initialization right in the class body! Sweet!
     int height = 5' 9";
     int weight = 160lbs;
     Color eyeColor = BrownColor;
}

PARTNERS