SystemClass* System;
bool result;
// Create the system object.
System = new SystemClass;
if(!System)
{
return 0;
}
// Initialize and run the system object.
result = System->Initialize();
if(result)
{
System->Run();
}
// Shutdown and release the system object.
System->Shutdown();
delete System;
System = 0;
return 0;
Why do you think he is making System a pointer, and would you normally do it this way as well? I don't see him copying or reusing the variable anywhere.
Without spending time going through the whole code there are few reasons I could immediately think of.
1. He specifically wants the instance to be stored on the heap instead of the stack. This could be to ensure he doesn't exhaust the available memory on the stack.
2. By using allocation and de-allocation he exactly controls when the constructor/destructor is called. The order of destruction and construction is often important when using third-party libraries or developing complex systems.
The snippet code you have provided only really leaves the possibility of #1, unless he is working up towards a more complex structure.
Thanks for the reply. I do have some follow up questions then.
In the code I posted above, would you make System a pointer as well, assuming that System is the container and controller for the rest of the program? How about using a smart pointer instead?
In this example because it's in the main method there is very little risk of forgetting to free the memory.
Personally I'd definitely use a smart pointer and reset it where he is doing the delete. Again this is to ensure order of destruction is maintained. (something like boost::scoped_ptr<> would be ideal)
Zaita's points are right on.... but unless SystemClass is huge, I don't see any reason why you'd use a pointer here. The likelihood of exhausting stack space with just a single object is very, very low.
This guy also does this:
1 2 3 4 5
System = new SystemClass;
if(!System) // <- System will never be null... so this check is pointless
{
return 0;
}
Which leads me to believe this guy's tutorials probably aren't the best.
That's not to say you shouldn't read the tutorial. I'm sure there's still merit to what he's doing. But he's definitely doing other things that are questionable.
Using a pointer still allows you to control the destruction time of the object. Again this example is so simple it's not likely an issue, but it still should be considered for future growth of the project :)
I notice the guy does that for most of the pointers. Not sure why he does that, but his tutorials are easy to follow and help set up DirectX pretty well, though I have changed the organization considerably. I will see if I can find other tutorials to double check my work to make sure I am not picking up bad habits.