Well...that changes things. Okay, I'll have a look as soon as I get back.
That would fit, since I was learning Java for a while a couple of months back. In that case, where should I declare the function? In a separate header, or in the same one?
Printable View
Well...that changes things. Okay, I'll have a look as soon as I get back.
That would fit, since I was learning Java for a while a couple of months back. In that case, where should I declare the function? In a separate header, or in the same one?
Where it belongs - meaning, if it's likely that you'd need it to be used in conjunection with whatever else is in that header, then there's no need for a new one.
A header-file should be relatively "complete on it's own". Having a whole bunch of little header files that you need to include all (or nearly all) of to make a useful program serves no purpose.
Obviously, if you have a large set of functions that belong together, then you would want to have that as a separate header, simply to keep the size of headers down. But that would be unusual.
--
Mats
Okay, one more question. I have a class that's made up entirely of static variables used throughout the program, such as screen size, number of objects, and general stuff like that. I'm next to positive that a class isn't the way to group them. So how should I? Should I make them global?
What exactly do you mean by "general stuff"?Quote:
Originally Posted by G4B3
Global variables are GENERALLY not the right solution. However, if you have a bunch of things that you need to access all over the program, making a structure that holds them, and have one global instance of that structure MAY be a solution.
Or make it a regular object, and pass it around (by (const) reference to the various places it is needed).
Or make it a singleton object.
--
Mats
How is that different from just having global variables?Quote:
However, if you have a bunch of things that you need to access all over the program, making a structure that holds them, and have one global instance of that structure MAY be a solution.
That's what I did in Java, but only because we can't have global variables in Java.
You can also prefix your globals with g_. Now, what's the difference again?
That works too - as long as it is actually done consistently.
As I said, ideally, you should have NO global data. But sometimes it's either awkward to get the right data from the place that knows the right stuff, to where it is needed later on (and it may need to persist across multiple object instance construction/destruction), so it is after all "better" to have a global collection of variables.
In my view, the standard method of a singleton class is pretty close to a global variable as well.
--
Mats
What about putting them into a separate namespace rather than a class?
(although, from the sounds of it, I think a class is the right place)