|
-
May 20th, 2009, 03:30 AM
#4
Re: Is this "good" coding style... ?
 Originally Posted by Stachelsk
I come from a heavy Java background, so I'm used to things cleaning up after themselves...
To build on treuss' last example, this can be achieved by a smart pointer (as mentioned by Speedo), assuming that your EngineController's derived classes have destructors that properly clean up after themselves (not with delete this, but say, by calling the necessary library functions), e.g.,
Code:
const unsigned int platform_osx = 0;
const unsigned int platform_win32 = 1;
// ...
#ifdef PLATFORM_OSX
const unsigned int platform = platform_osx;
// ...
#endif
std::auto_ptr<EngineController> createController()
{
if (platform == platform_osx)
return std::auto_ptr<EngineController>(new OSXController());
else if (platform == platform_win32)
return std::auto_ptr<EngineController>(new Win32Controller());
// ...
}
int main()
{
std::auto_ptr<EngineController> controller(createController());
// ...
controller->startEngine();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|