Skimming through some code I noticed something I honestly can't wrap my head around in a constructor.
class Terrain
{
public:
Terrain(int movementCost,
bool isWater,
Texture texture)
: movementCost_(movementCost),
isWater_(isWater),
texture_(texture)
{}
... //More code
What sort of wizardry is this? Are those foo_(foo)
representing foo = foo_
?
Asked
Active
Viewed 557 times
0

geekfreak.xyz
- 90
- 1
- 9

Francisco Garcia
- 35
- 4
-
See also: http://www.parashift.com/c%2B%2B-faq-lite/init-lists.html – Ferdinand Beyer May 08 '14 at 17:17
-
1Please read a basic tutorial or at least the C++ faq lite or some such. – Deduplicator May 08 '14 at 17:31
1 Answers
2
This is a c++ initialiser list. You have it almost right, foo_(foo) is equivalent to foo_ = foo;
This is useful for when you have a member variable that does not have a default constructor. Without this feature, you would have to make it a pointer.
The initialisations are also executed in the order that the members were declared in the class defenition, not the order they appear in (which should be the same as a matter of style, but isn't necessarily)

wheybags
- 627
- 4
- 15
-
This is also required when you have const member variables and when calling base class constructors. – Chris Drew May 08 '14 at 17:24
-