Archived

This topic is now archived and is closed to further replies.

clrscr

Constructors Initialization Stage

Recommended Posts

clrscr    158
Is there a real speed advantage to using the Initialization stage as opposed to doing it in the function? Its not knowing the path but walking it.

Share this post


Link to post
Share on other sites
Cedric    158
It depends on the kind of initialization that you are doing. Most of the time, worrying about this is considered premature optimization.

Unless you have a very good reason for doing it in a function, I would consider it bad style anyway.

Cédric

Share this post


Link to post
Share on other sites
Oluseyi    2103
quote:
Original post by clrscr
Is there a real speed advantage to using the Initialization stage as opposed to doing it in the function?

Consider robustness before speed. Using the Resource-Acquisition-Is-Initialization (RAII) paradigm (constructors always return ready-to-use objects) ensures that a programmer never obtains an object in an inconsistent state - a healthy and desirable objective.

Also, miniscule speed improvements are useless if the code in question isn''t a performance bottleneck.

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by clrscr
Is there a real speed advantage to using the Initialization stage as opposed to doing it in the function?

Yes. If you initialize members within the body of the ctor, they have already been default constructed. If you do it in the initialiser list, you don''t pay for that extra default construction.

Share this post


Link to post
Share on other sites