BeanDog:
#2 STL usage
Anytime you need to manage a collection of objects, like linked lists, queues, arrays, maps, etc. STL's got 'em. Besides, STL is coded in templates (hence the name Standard Template Library), and it's very fast because it uses templates. Also, STL doesn't require modifying your classes to work with its containers (except maybe a copy constructor/assignment operator), which keeps your code pretty small.
So say you have this class:
struct color{typedef byte unsigned char;byte red;byte green;byte blue;};
In order to create a doubly-linked list with it in C, you would normally do something like this:
struct color{typedef byte unsigned char;byte red;byte green;byte blue;color* prev;color* next;};
With STL, you can keep that stuff out of the class definition. STL automatically creates classes to hold that data for you, so when you need a list, you just do this instead:
list<color> colors;
And that's it. You can iterate through it, or use the overloaded operator[] to use it as an array. When you want to use that color class with a dynamic array instead (for faster access), you would do this:
vector<color> colors;
Of course you can use that vector just as if it were an array of color objects, but you can also use the vector member functions. vector contains functions to set the array size, insert members while automatically re-sizing the array, calculating the fastest new size, etc.
STL takes care of all the complicated stuff you really don't want to worry about when you just want to store your data. Of course, all of the insertion functions tell you what basic formula you can use for insertion times, etc. so if you need the STL classes for performance-critical code the information is all there.
BTW,
You could write a container module in C, but it's either not very flexible (can use only one data type), or it's not type-safe (uses void*). Besides, all the casting from void* is going to slow down performance a bit, no?
You could use macros in C to emulate templates, but that is messy. Besides, without the overloaded operators the client code is less intuitive and more complex. And what about inline functions? You would have to use macros for those, too, if you want the speed.
STL is incredibly fast and very easy to use. Learning it is another matter. I suggest you find a reference on the web or in a bookstore, or post questions about the different classes here.
Basically, learning your first STL class is hard, but all the other classes use similar concepts so you get a feel for the design of the library. Start with vector, since it's basically an array (which every programmer should be familiar with) and you don't have to learn how to use iterators.
Also part of STL is the stream operators that everyone seems to pick apart. (hey, it's their loss)
Basically, overloaded the operators and using templated operators allows easy, type-safe reading and writing. Plus you can overload those operators for your own data types, so their extensible. Streams are also very flexible concept, as they can send the data to a file or across a network (provided a network stream is written) with no change in the usage of the stream and thus no change in your code.
When I wrote my 2D tile-based map editor, I used STL streams. Simply write an input and ouput operator for each class, and then you can use those operators just like with the intrinsic types.
When I need to read or write a map object from a file (including tile layers, image file names, objects, scripts, etc.) I simply do this:
file << my_map;// sometime later...file >> my_map;
And all the details are handled by the operators that I have written. Simple, no?
#3 switch vs. virtual
Here's what I do. If I have to switch on the "type" of the object or something used to indicate the "type" (like an ID byte), then it's a good time for inheritance and virtual functions.
If you find that you cannot make the virtual functions work, or it's very messy, then you might need to re-think your design. NP! That just means that somewhere along the line you have created more work for yourself than is necessary, and once you find that you have both better code and better understanding of the problem domain!
Happy Coding!
-
null_pointerSabre MultimediaEdited by - null_pointer on July 16, 2000 8:24:25 AM