Advertisement

Unnecessary C++ features?

Started by July 13, 2000 07:53 AM
70 comments, last by null_pointer 24 years, 5 months ago
This is turning into quite a nice thread!


c++freak:

I meant any keywords, not all keywords.

viz. template, typename, class, register, etc.

BTW thanks for trying to stay away from C vs. C++! I just want people to discuss C++ by its features and not get into a language war.


Whirlwind:

If you go into your compiler documentation, there should be an explanation of each STL class and its member functions, plus some examples (which can be accessed through the help for the member functions). You do have to hunt around a bit for it though.

Basically, STL is a lot of typedefs and some very useful classes. They start small (like pair, auto_ptr, etc.) and keep building upon those objects. It is a very well designed library - plus it reduces the size of my code quite a bit. Instead of writing a map class myself to hold the images in my map editor, I use this code:


map&ltunsigned int, image> images;



which enables me to place an int in each of my map tiles and objects, indicating which image to use when I want to draw that tile or object. I then go to the map of images, give it the ID, and I get the corresponding image. It''s quite a lot better than tracking objects based on their pointers, I assure you:


display_surface.copy(images[ID], tile.x, tile.y);



Not only that, but I am using vectors to hold the tiles and objects, which are -fast-. I''d show how small the actual source for the map editor is, but I can''t release it to the public. I just wanted to give an example of how it could be used.

Also, when I wrote a font engine, I used this code in each font object:


map&ltchar, image> images;



which allows me to go through an STL string, use it as an array, and print out the corresponding image from the map class. That is:


for( int x=0; x < mystring.size(); x++ )
display_surface.copy(images[ mystring[x] ]);



Of course, you need to add code to keep track of the new position of each letter as you are drawing it, but you get the idea: find the current char, then get its image, and copy it to the screen.

Then there is always the optimization stage. Since I had the same amount of characters (256) and the same range (0-255), I just used a vector that contained 256 images instead of mapping characters to an image with the map class.

IMHO, since all programming is manipulating data, and STL is a powerful tool for manipulating data, STL is invaluable for C++ programming. Of course, that''s just my opinion.


Happy Coding!




- null_pointer
Sabre Multimedia
I am a book man myself. I hate digging for information. A physical list would be really nice to find in a book. The online documentation ''craze'' doesn''t do me any good. That would be another non-C++ specific thing to go away. They get rid of a paper manual and don''t bother reducing prices. STL online references are near useless to see if something exists that you want to do.
Advertisement
Hmm, I found the references useless. Basically a pointer to a pointer is passing by reference so if you are too lazy to type out ** then I guess it isn't. It adds more confusion than anything, becuase it isn't a pointer and it isn't a variable, so it has no logical place.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Edited by - immaGNUman on July 13, 2000 12:24:01 PM
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
On the ++, -- operators

One thing about the ++ -- += -= etc. operators, when C was first developed, compiler optimization wasn''t very far along. These operators were added to take advantage of specific PDP instructions (can''t tell you which ones since I cut my teeth on the VAX processor and not the PDP.)

Many times, the optimizing compiler will optimized out such things as ''a = a + 1''. With today''s compilers, there is really no telling when the compiler might convert a ''a = a + x'' expression into a ''a += x'' or visa-versa.

Tim
Personally I think they implemented iterators in STL very well. On another note though, I can''t STAND the implementation of templates in Visual C++. I can''t even put declarations and definitions in separate files, which really bothers me...
The reference operator (&) is VERY important in C++. You really see it shine when overloading operators. It also allows you to treat objects more consistantly in the arguments lists. If you look at my code, you will see a lot of stuff like ''const CMatrix &m'' in argument lists. This allows me to conceptually pass a matrix around as if it was just another simple variable and not some large structure.

Tim
Advertisement
Placing template function definitions in a different source file was not even allowed until recently in the standard (if I have my dates right). Currently, most compilers do not support this. One nice thing on the horizon is template libraries without sources.

It is still going to be a while before the compilers catch up with the standard.

Anybody know what compilers support external template definitions?

Tim
The good thing about the language is you don''t have to use the parts you don''t like.
quote: Original post by TimSmith

Anybody know what compilers support external template definitions?



I know borland''s free command-line compiler does a MUCH better job at the ANSI templates standard than Visual C++, but I think that even that one doesn''t support external template definitions.

On the topic of references, it''s been said in this thread already, but they do not provide additional functionality, they provide transparency for a certain concept - passing by memory location.
I think that it''s a legacy thing though, really, every argument could be a const ref by default, unless you explicitly say it''s a variable parameter ( by making it a pointer, or whatnot ). Just one of the things you have to live with when using C++ .

[personal thoughts coming]
I personally think that anything that is NOT a const reference, is dangerous. You need it for operator overloading of course, but that''s really the only place I wouldn''t think twice or three times before using a non-const reference. The caller side cannot see if the argument will be changed or not.
Hmm, that makes me wonder, can you cast something to const?
for instance foo( (const)i ); which would generate a compiler error if i was changed anywhere in foo()?
Okay, I''m on my weird track again, ignore me




Give me one more medicated peaceful moment.
~ (V)^|) |<é!t|-| ~
ERROR: Your beta-version of Life1.0 has expired. Please upgrade to the full version. All important social functions will be disabled from now on.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
It''s impossible to think(altought you did) that C++ can be worse than C. Like somoene said(I do not remember who), C++ was built in C. And C++ is a upgrade of C! It''s got to better.

I say the upgrade is better than the original version ''cuz it wasn''t made by Microsoft, the WindowsNT service packs are examples of this, the upgrades fix some errors and take a hundred new ones.

Thanks, Arthur(rockslave)
import money.*;#include "cas.h"uses bucks;

This topic is closed to new replies.

Advertisement