On Mar 30, 6:18 am, "Daniel T." <danie...@earthlink.net> wrote:
mike3 <mike4...@yahoo.com> wrote:
I was wondering where, if at all, it's "OK" to use "bare"
procedural stuff (i.e. not part of objects, but bare functions) in
C++. For example, I've got these routines that implement a random
level generator algorithm (for a game). Is it OK to just do this
procedurally, like have a "MakeTunnel" function to make tunnels in
the map (which is an object, by the way), a "MakeRoom" function to
make rooms, etc., or should one put that in objects of some kind?
If so, what kind of way would be good for this? This one seems
tricky...
I'm going to assume that what you are asking is if it is OK to have
global functions. The answer is of course, even the standard library
is full of them. However, the best code is modular in nature, and
the most natural way to express a module in C++ is by using a class.
I suggest you read, or re-read, chapter 2 of "The C++ Programming
Language" by Stroustrup. He gives a good overview of the various
programming paradigms.
If you find yourself writing a bunch of switch statements, or if..
else chains that all use the same condition, then you should
probably introduce more inheritance.
So then what, you have classes without data members then? It doesn't
make sense as a data type, it's an algorithm after all, and there
aren't any "global" variables that need to be tucked way, just
functions.
Is it OK to have it contain lots of internal functions that are used
inside it (e.g. the MakeTunnel stuff, etc.) but aren't for calling
from outside (the only ones that'd be available to the outside would
be the main generator routine which you punch in a level type to
generate and the necessary parameters (including a map to use) and it
generates that level on that map)?
So then if I had something like this:
class LevelGenerator {
private:
... (various functions like MakeTunnel, etc. all "static") ...
public:
static GenerateLevel(LevelMap&, LevelType); // Generates the level
};
is that bad?
Show me the implementation of your GenerateLevel function. If you want