views:

60

answers:

1

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

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

or do I have to do it in the body of the constructor?

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

If it matters, the foo class is derived from another class and does have virtual methods. In addition, no exception handling is allowed in our app.

+4  A: 

If random cannot throw (hard to believe that it could), there is no problem with this. Side effects are allowed in constructor initializers. It's good practice to do any initialization there, if it takes only little code.

Johannes Schaub - litb
That's what I was looking for. Thanks!
Michael Dorgan
Even if random could throw an exception, there's a special construct called the function-try block that allows you to catch the exception.
Ken Bloom
Again, in my case it doesn't matter as exceptions are completely disabled in my app.
Michael Dorgan