No, you don't.
Yes, I do.
Trust me on this. You can't learn C++. No one can learn C++.
You mean you can't learn it.
No, I did learn it. I spent a long time learning it. And for a while I knew it as well as anyone. But I don't work in it any more and really no one works in C++ any more.
There's a whole buncha code written in C++, dopey. People work on it.
AlternateHardAndSoftLayers ate C++ alive. Small standalone optimizations of SoftLanguage features still get written in C++. But why do you wanna write them? Learn ruby or smalltalk or python - all the same thing now really - and forget the C++. Or go learn C#. Or even <shudder/> java. But really, if you don't already know it, learning C++ is about as useful to you as learning Latin.
...Unless you're developing action games, or embedded apps, or any other environment where you need high performance and a giant monolithic interpreter is not an option. -- AC
[True. I just asked one of my academic colleagues -- whose industrial background and current teaching & research area is games, in particular the console and PC games industry -- to what degree one could get by without C++. His answer was that "They (the industry) won't even look at you." I asked him about use of Ruby, Python, etc. They are being used, but only in conjunction with C++. In such cases, you're expected to know both the hard and soft layers. You still need, at the very least, to have C++ on your CV/resume.] -- DaveVoorhis
People new to programming sometimes have the impression that C++ is the modern programming language. If you don't already know an OO programming language, check out IwannaLearnPython, FreshmansFirstLanguage, or AlternativesToCeePlusPlus. C++ is not a good learning language. That's not to say C++ is a bad language - it's very good at what it's designed for (compatibility with C, and execution speed).
Compared to CeeLanguage, CeePlusPlus is <fill in the blanks> (a CeeVsCpp? page might be useful...)
So you wanna learn CeePlusPlus. What you should first answer is: Do you want to (or should) learn C++, or a mishmash of C++ and CeeLanguage, or EarlyCeePlusPlus, or LateCeePlusPlus, or another language entirely?
Huh? If I don't *already* know C++, how in the world am I supposed to answer this question without flipping a coin?
Will you be working with legacy mishmash code or coding from scratch? Are you coming to C++ from previous knowledge of C, coming from other programming languages, or coming as a first-time programmer? Also, what does your compiler expect? If you're not so sure you wanna learn C++, there are AlternativesToCeePlusPlus, you know.
Most people learn faster alternating between hand-on coding and reading, rather than reading some thick book in its entirety before touching the keyboard. Get a decent C++ compiler and C++ IDE (they will also work just fine for compiling plain C programs), and you can start pounding out code with CeeProgramsForBeginners. Then grab one of these books and go through the exercises.
Books
#include <iostream> using namespace std; template <class T> bool ansisupported (T x) { return true; } int main() { if (ansisupported(1)) cout << " ANSI OK "; return 0; }
CeePlusPlus subtleties rescued from defunct CeePlusPlusSubtleties
while(*dest++ = *src++);Very bad style. Always, always, always use "continue":
while (*dest++ = *src++) continue;And it is idiomatic more than it is subtle.
For this particular code, it would be better (and faster on most machines) to use the library routine strcpy(). Even better - the library routines strncpy(), or even better strlcpy(). See CeeLanguage for details.
But strlcpy() is not a part of C Standard.
For the "real" subtleties, there are many fine books such as StructureAndInterpretationOfComputerPrograms, ModernCeePlusPlusDesign, OnLisp and the likes. Of course, they are in languages that allow and encourage subtle things, and more importantly, they're not subtleties for the sake of being "subtle", but smart, elegant designs that make code better and the life of the coder enjoyable.
A few subtle, difficult, topics for CeePlusPlus:
Another subtlety, because arrays are SyntacticSugar for pointers in C/C++, is that square brackets are commutative. a[5] = 5[a]. The former is *(a+5), the latter *(5+a).
No, that is not true - arrays are not *just* SyntacticSugar for pointers, they are very different. Google the C++ Usenet groups for a comprehensive discussion on why they should not be considered as the same.
But 5[a] is universally considered bad C/C++ style, so it's pointless. The fact that a[5] is *(a+5) is the main point, and it's not all that subtle.
An array is a constant, you cannot perform a++ (referring to the a[] above), unlike a pointer which may be incremented, thus the difference is not of syntactic sugar for this reason. -- DavidMXGreen
This doesn't show that it's not SyntacticSugar. You can't perform a++ on a const pointer either.
Simple solution: don't use arrays. When coding modern C++, arrays should be thought of the same as the "unsafe" operations in C#, or modifying builtin objects in Python - something you use only when you must. In the case of C++, this is during hardcore optimization, or if your compile times are getting too freakishly long, or when implementing your own containers from scratch when the standard library is unavailable. Otherwise, use Vector<>. C++ is not a horrible language if you learn modern C++ with heavy use of BoostLibraries and StandardTemplateLibrary (STL) - in the end, it feels like a somewhat verbose C#2 with some weird extra gotchas (the real problem), annoying layout (the header/linker crap) and stack-based destruction (actually allocating objects on the heap is more rare than you'd think in well-structured C++). If you stick to some solid "best practices" and stuff most of the old C language in a box labelled "unsafe" you can do fine.
As far as you have read through all this page, either you would escape threatened, or you would conclude that all these people are so freaked out. You started as a genuine novice that wanted to learn, and ended in an odd discussion about 5[a].
My relationship with C++ is like the one of gollum love/hate to the ring: "We swears to serve the master of the precious. We will swear on... on... the precious!"
I started 10 years ago dreaming about C++, learned it, or still learning it. Is the most powerful and more odd language you may ever encounter. The language itself contains 5 or six different dialects variants, and you can build your own. The most beautiful part of it is that you can design within the code. The ugliest is the standardization, why the heck we are standardizing && and we don't have a decent standard library for widgets, xml or http? Is that possible that nobody has ever written a clean implementation of these in the last 20 years?
If you manage to become competent in C++ and even have some expertise in it, the effort that went into doing so won't be wasted even if you never use the language again. After C++, learning any other programming language will seem easy.
See also CeePlusPlusRoadMap