I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not.
Should I only use it for attribute initializations, calling parent constructors etc.?
Or might I even put more complex functions into them like reading and parsing configuration data, setting up external libraries a.s.o.
Or should I write special functions for this? Resp. init()
/ cleanup()
?
What are the PRO's and CON's here?
I figured out yet that for example I can get rid of shared pointers when using init()
and cleanup()
. I can create the objects on the stack as class attributes and initialize it later while it is already constructed.
If I handle it in the constructor I need to instantiate it during runtime. Then I need a pointer.
I really don't know how to decide.
Maybe you can help me out?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…