Init/Shutdown methods

Started by
1 comment, last by SiCrane 11 years, 5 months ago
I am going through a tutorial and I noticed that all the classes have initialize and shutdown methods on top of the constructor and destructor. I understand the purpose of an init, where you may need to declare but not actually define stuff yet, but the shutdown confuses me as you could just do all that stuff in the deconstructor. Is there any reason to use a shutdown, or even an init for that matter? Or should I be putting that stuff in the constructor/destructor?

I should mention that the "stuff" in question is mostly DirectX stuff.
Advertisement
Yes, in general, it should be better to leave those in ctor/dtor.

Problem is that (I guess) MSVC didn't implement proper exception throwing for a while, therefore making RAII much, much harder to use. Consider:

int *avoidPlainArraysWhenPossible = new int[RequiredValue()];
// (1)

In old visual studio versions, [font=courier new,courier,monospace]new [/font]would return NULL on failure. Therefore at point (1)

  1. In compliant C++, you could just assume the array to be allocated
  2. In MSVC, you couldn't.

Furthermore exception handling was a bit problematic, often disabling some optimizations and generating a lot of extra code.
Those issues are pretty much resolved in 2010, but that's the likely reason for which they use [font=courier new,courier,monospace]Init [/font]calls instead of just relying on the ctor.

There's a complication however. Ctors cannot rely on internal virtual functions. That is, if your initialization is non-trivial, you cannot do RAII.
It is often possible in my experience to refactor those objects to not rely on virtual functions at construction by providing them with adeguate external support. But it takes effort.

Dtors are "worse" as they cannot be parametrized. The only way to provide them with reference to ... say Vertex Buffer managers... is to put a reference explicitly. This does not always makes sense.

For general code, your 1st choice should always be ctor/dtor only and pure RAII but this is just a sanity guideline. Alternatives are allowed and might make sense.

Previously "Krohm"

You may also want to see the discussion in this recent thread.

This topic is closed to new replies.

Advertisement