Is it possible to call a function to initialize a class in the ctor list?

I have an Angle class that I want to initialize with a random value. The Angle constructor can take an int from the random () function. Is it safe to put this call in the ctor list:

foo::foo() : Angle(random(0xFFFF)) {...}

      

or do I need to do this in the body of the constructor?

foo::foo() { Angle = Angle(random(0xFFFF)); ...}

      

If it matters, the foo class is derived from another class and has virtual methods. Also, exception handling is not allowed in our application.

+2


a source to share


1 answer


If you random

can't quit (it's hard to believe it's possible), there is no problem with that. Side effects are allowed in constructor initializers. It is good practice to do any initialization in there if only a little code is needed.



+4


a source







All Articles