If a constructor has to do a lot of processing to set its initial state, that’s a code smell.
When a constructor is used for the main processing part of a class, such the code requires an instance of Foo to spend a lot of cycles computing Bar, that’s almost a sure indicator the design needs improvement. It may not need a lot of improvement, but it almost surely needs rethinking.
Constructors should only set state, really. If arguments
must be passed, those should be easy to instantiate themselves.
I’m not the only person who believes doing work in constructors is a flaw.
You can google “doing work in constructors” to find like thinking.
Really bad smell
Check this out:
Foo::Foo(Bar b) : b(b) {
private_m = Baz::some_wackadoodle_preset_structure;
};
I recently had to find a way to test such a constructor, where the constructor depended on some static (global) value of another class, which was initialized elsewhere.
Very painful.