0

I'm currently reading some C++ code, and can't find an explanation for the following syntax.

The function rng_fn is just a random number generator that I want to seed with the same value every time the constructor is called. What does the rng_fn(nullptr) do after the colon, and does this override what happens inside the constructor?

#include Object.h

Object::Object() : rng_fn(nullptr)
{ 
  unsigned int seed = 1;
  rng_fn(seed);
}

(I have removed everything from the constructor except for the lines I don't understand.)

Orca
  • 177
  • 9

1 Answers1

0

What you are looking at is an initialization list. The members of the class are listed in order of declaration and constructed in place before the body of the constructor is even run.

BugSquasher
  • 335
  • 1
  • 13
  • I see, so any time the random number generated is used after the first two lines of the constructor, it will be seeded by 'seed'? – Orca Mar 27 '19 at 08:43
  • Well this case is particular. If rng_fn is a function pointer, it will be initialized by a `nullptr` and then it attempts to dereference the function pointer and pass the seed to it. – BugSquasher Mar 27 '19 at 14:38