Jump to content
  • Advertisement
Sign in to follow this  
mystb

switching to OOP/C++

This topic is 3553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm having a little problem dealing with some specific "objects" implementation. I had a generic debug collection of functions that I used in several projects. They were simple C functions stored in a c file, with a local static struct used to hold configuration data. * The benefits of this approach (1) - I don't need to initialize a object. If I just accept the defaults, I just call spew("yodiyodi") and this is logged to a console and a file. (2) - I don't need to have a global var with the "debug object". Switching to a OOP approach gives some great benefits. For example, I can create a debug class that works with streams, and in runtime add/remove streams from the object, thus being able to log to several files at the same time, console, etc, etc. But, my approach to (1) and (2) mentioned above will be: - At the very beggining I would have to instantiate a debug object. - I would have to declare a global var to be able to access this object from everywhere Or, I could add some C style functions that do that for me (like: c_function_create_debug(); c_function_get_debug()->spew("fjkds")) This is just a example. What I'm trying to find is a way to code classes/objects that are instantiate just once, and need to be accessed throughout the rest of the code. Hope I made myself clear :)

Share this post


Link to post
Share on other sites
Advertisement
As long as your class is not abstract and has a public constructor, you can create global instances of it either directly, or as an instance function:

Object &getInstance()
{
static Object instance;
return instance;
}


However, the point of switching to an OOP approach would be to be able to create more than one object—otherwise, a plain old global module (like those created in C) would be more than enough to allow what you're planning to do.

Share this post


Link to post
Share on other sites
thanks for your input.

That was my point. For me it seems that in these cases, the module approach seems a better solution. I just though I could be missing some concept that allowed a better OOP solution.


Share this post


Link to post
Share on other sites
To the problem of "I want a globally accessible log", a classic procedural module is the simplest and most effective solution (globals and object-oriented programming don't mix very well). If you had another problem, such as "I want to be able to specify different logging systems for different objects", then an object-oriented solution might be preferred.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!