Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Init/Shutdown methods


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 viper110110   Members   -  Reputation: 691

Like
0Likes
Like

Posted 21 October 2012 - 11:54 PM

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.

Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 3183

Like
5Likes
Like

Posted 22 October 2012 - 12:39 AM

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, new would return NULL on failure. Therefore at point (1)
  • In compliant C++, you could just assume the array to be allocated
  • 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 Init 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.

#3 SiCrane   Moderators   -  Reputation: 9629

Like
1Likes
Like

Posted 22 October 2012 - 07:49 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS