   Useful or Non-useful Uses of Classes (C++)
  What is your opinion on the following circumstance:- You have 'x' number of variables that 'y' number of functions need to have access to.- Other parts of your program must access 'y' functions but do not need to access 'x' variables (and should not be able to access 'x' variables). Other functions that act on 'x' exist as well, but do not need to be visible to the rest of your program.- The 'x' variables must be "initialized" in some way, and "finalized" in some way.- Only one "instance" or "copy" of these variables and functions is necessary.Would you create a class which holds 'x' variables and 'y' functions, where 'x' variables are private and 'y' functions are public?Would this class be a singleton since only one "copy" of these objects are needed?Would you just create a header file which contained the function prototypes for 'y'; and a source file with the variable definitions 'x' (of course the definitions of 'y' would be there also)?I tend towards the latter, but I always feel pressured to be "Object Oriented", and everyone always says "Stay away from global variables"! However, in a case like this, using a class would be redundent wouldn't it? Am I right in the assumtion that having a class for this circumstance is actually not object oriented at all?My question boils down to ... If I only have one instance of a class (and that is all that's needed, and I know I'll never need to have two instances of this class), and nothing will ever have to inherit from this class, is there a point in even having this class?I hope I said what I wanted to say, If I didn't make sense, please say so and I will attempt to clarify.   
  "How many c++ programmers does it take to screw in a lightbulb?..100.  1 to screw it in, 99 to tell him or her how to do it better"-from some topic we had full of jokesThere are plenty of tried and tested concepts when using objects and classes in c++, but sometimes what you need is some common sense. Do what you think is right. If you do it "wrong", you'll figure it out eventually, and next time you won't do it.If you dont figure out that it is wrong, how wrong can it be?   ;  Sure. Having the group of variables in a class improves orginization, clarity and allows easier addition of group functions such as serialization. Though I [an unwashed heathen] would probably just use a global variable for the class rather than a singleton in most cases.   ;Quote:Original post by TelastynSure. Having the group of variables in a class improves orginization, clarity and allows easier addition of group functions such as serialization. Though I [an unwashed heathen] would probably just use a global variable for the class rather than a singleton in most cases.Me too.  I surely would use the class to keep the variables and functions in and then just use a global variable for the class.  Or you can use a pointer that you pass between functions, but it depends on you.  In most cases, I use the pointer to the class and create it dynamically, but I keep the pointer global so I don't have to pass it from function to function.  But that's just me.   ;  I think global variables are fine until they get split up amongst many files.  If that's the case, you could try a static class.   ;  cin and cout are globals you know ;) On the other hand, they're global *object instances* ;) There are many ways of doing things, and you can refactor between them, too. You could for example make a struct where the data is public, but the *struct's definition itself* is hidden (not published in the .h file), and then just instantiate it once in the implementation. Then provide free functions that access/mutate the shared struct. Then later you could make it a full-fledged class, publish its definition, give it meaningful member functions, and have the old free functions implemented in terms of member functions on a global (file-scoped, not 'extern'ed) instance.   ;  Thanks for the replies guys.Quote:Original post by ender7771"How many c++ programmers does it take to screw in a lightbulb?..100.  1 to screw it in, 99 to tell him or her how to do it better"-from some topic we had full of jokesThere are plenty of tried and tested concepts when using objects and classes in c++, but sometimes what you need is some common sense. Do what you think is right. If you do it "wrong", you'll figure it out eventually, and next time you won't do it.If you dont figure out that it is wrong, how wrong can it be?Most of my time coding I usually think about if I am doing something right or not, so much I never actually get anything done. I'll just keep going on, and if I find some horrible horrible problem later on, it will be a lesson learned.