26 Replies - 11452 Views - Last Post: 23 February 2011 - 04:12 PM
#16
Re: [Week 4] Sample Game Programming Interview Question
Posted 22 February 2011 - 11:57 AM
#17 Guest_hype261*
Re: [Week 4] Sample Game Programming Interview Question
Posted 22 February 2011 - 01:27 PM
diego_pmc, on 22 February 2011 - 11:57 AM, said:
Generally speaking to deallocate the memory from a Singleton you have a seperate static method called release. This method just deletes the static instance of the Singleton. This brings about other design decisions like when do you delete your singleton.
#18
Re: [Week 4] Sample Game Programming Interview Question
Posted 22 February 2011 - 03:53 PM
Quote
#19
Re: [Week 4] Sample Game Programming Interview Question
Posted 22 February 2011 - 04:44 PM
Singletons are useful for managing tasks that multiple objects need to share.
I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.
Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.
Another great question. This is one of my favorite topics.
#20 Guest_hype261*
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 07:05 AM
shintetsu_80, on 22 February 2011 - 04:44 PM, said:
Singletons are useful for managing tasks that multiple objects need to share.
I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.
Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.
Another great question. This is one of my favorite topics.
Generally speaking you don't need the assert to ensure that memory was allocated with the new operator. If memory allocation fails then a bad_alloc error will be thrown. You can make the compiler return 0 on memory allocation failure by using std::nothrow
#21
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 07:29 AM
template<class T>
class Singleton
{
public:
static T* Instance()
{
// Meyers singleton
static T obj;
return &obj;
}
};
Having it have access to the singleton class it is a friend of is exactly what you want it to do. All it really is is a way of saving you a little implementation and this is definitely a case where I would throw caution to the wind.
#22
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 07:36 AM
hype261, on 23 February 2011 - 07:05 AM, said:
shintetsu_80, on 22 February 2011 - 04:44 PM, said:
Singletons are useful for managing tasks that multiple objects need to share.
I've used this approach to reduce calls to binding textures and loading sounds. For example a texture manager would keep track of the currently bound texture so when you request a new texture from the texture manager the manager would know if it needed to bind a new texture or just hand back a handle to the currently bound texture. In this case any object that uses a texture is going to share from the same pool of textures. No need to take up space with multiple copies of the same texture.
Another useful implementation of a singleton would be a logger. Only one object needs to know how to log files and any object that needs to log something would just send it's log message to the logger.
Another great question. This is one of my favorite topics.
Generally speaking you don't need the assert to ensure that memory was allocated with the new operator. If memory allocation fails then a bad_alloc error will be thrown. You can make the compiler return 0 on memory allocation failure by using std::nothrow
That's true and I did consider removing that from my sample but since I had just pulled it out of one of my old test projects I developed a while back to help myself understand singletons decided to leave it in.
I suppose the real question would be is it wrong or is it just a matter of coding preference?
#23
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 09:36 AM
#24
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 10:27 AM
This post has been edited by diego_pmc: 23 February 2011 - 10:38 AM
#25
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 10:46 AM
diego_pmc, on 23 February 2011 - 05:27 PM, said:
That's because they aren't an improvement to the concept of a singleton. look at it this way, in a game you are only going to implement certain aspects of the game engine (UI, camera, IO, logging, etc.) as a singleton, and since you will be creating a class for them anyway you might as well implement the class as a singleton.
#26
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 02:20 PM
#27
Re: [Week 4] Sample Game Programming Interview Question
Posted 23 February 2011 - 04:12 PM
|
|

New Topic/Question
Reply




MultiQuote






|