|
-
November 29th, 2012, 02:14 PM
#4
Re: Vector iterators
 Originally Posted by raptor88
My basic question was whether Method-3 should always work under the conditions I set.
You can take any piece of C++ code, regardless of what it does, and make it work exclusively with compiler X, Y, or Z. When you try it with Visual Studio Express 2010, did it work? If it did, then it worked for that compiler. However, how do you know that Service Pack X of Visual Express 2010 will make the code you have now faulty or uncompilable? What if you want to get Visual Studio 2012, and the code no longer works correctly?
The problem with the general question of "if I have compiler X, and did things this way in C++, would it work?" is as I stated. That piece of code that happens to work for compiler X may not work for compiler X, version 2.0, 3.0, 4.0, etc. (assuming you're using version 1.0).
One classic case of this is assuming that vector::iterators (since we're talking about iterators) were really simple pointers underneath the hood. The Visual C++ 6.0 compiler was the most popular C++ compiler of the late 1990's, and a lot of code that used vectors used shortcuts in the code, assuming that vector iterators were really pointers. This saved typing the whole "iterator" keyword, or saved creating a typedef, and saved having to declare types correctly.
Then here comes Visual Studio .NET, 2002, 2003, 2005, etc., and guess what? All of that C++ code that was created with Visual 6.0 that assumed vector iterators were pointers no longer compiled! Do you know how many Internet sites still have faulty Visual C++ vector/iterator code, all because the author was using Visual C++ 6.0 and took that fatal shortcut? The ironic thing is that if the coder using Visual C++ 6.0 didn't take shortcuts, and instead declared the vector::iterator correctly, used the correct types, assumed that vector iterators were not pointers, etc. that the code would have still worked using Visual C++ 6.0 and in any future version of Visual Studio.
So if you came to the Non-Visual C++ forum and asked "if I used iterators this way by assuming it is a pointer, and I'm using Visual C++ 6.0, and..."), then yes, the code would "work". But are you really learning correct coding? So please learn from this classic mistake -- never code something because it's "easier", "less typing", etc. Always write correct code with the future in mind -- if you know you're taking shortcuts because right now your compiler accepts it, don't take that shortcut.
In addition, many "Lint" tools and compilers where some warnings are considered errors would reject your Method 3. A size_t is a size_t, and may not be an unsigned int, causing the lint tool to report an error, or the compiler to not generate object code.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 29th, 2012 at 02:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|