I disagree. If you want to know how a linked list works, by all means write one yourself. But learning the standard library of a language is as important as learning the language itself, so you should use it in non-trivial programs.

Well...that changes things. Okay, I'll have a look as soon as I get back.

Quote:

Originally Posted by CornedBee

You're thinking like a Java programmer. C++ is a multi-paradigm language. You're supposed to pick the best from every paradigm. And having stuff that is independent of an object being done by a free function is the right thing to do.

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?

05-21-2009

matsp

Quote:

Originally Posted by G4B3

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

05-21-2009

G4B3

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?

05-21-2009

laserlight

Quote:

Originally Posted by G4B3

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"?

05-21-2009

matsp

Quote:

Originally Posted by G4B3

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?

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

05-21-2009

cyberfish

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.

How is that different from just having global variables?

That's what I did in Java, but only because we can't have global variables in Java.

05-21-2009

matsp

Quote:

Originally Posted by cyberfish

How is that different from just having global variables?

That's what I did in Java, but only because we can't have global variables in Java.

It is different because you can EASILY search for all references to "theglobaldataobject", plus given a correct name, it makes it obvious that it is a global variable.

--
Mats

05-21-2009

CornedBee

You can also prefix your globals with g_. Now, what's the difference again?

05-21-2009

matsp

Quote:

Originally Posted by CornedBee

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

05-21-2009

cpjust

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)

05-21-2009

matsp

Quote:

Originally Posted by cpjust

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)

I don't think namespace is in any way BETTER than a class or struct, really.