Most opinions on Stack Overflow seem to voice a similar opinion: C++ is "harder" to use and it is a "bad" choice for a beginner. Speaking as someone who has been learning C++ as a first language, on my own time as a hobby, for a few months, it doesn't seem all that hard. Admittedly, the first time I took a look at pointers and their relationship to raw arrays, the reasoning behind them seemed flimsy. That was before I created my first stack.
In fact, when I look at C# or Java code, it seems restrictive - why should I have to write a class to print "hello world?"
I guess my question is: what exactly (actual features; don't give me broad generalizations, please) makes C++ harder/bad for a beginner, like me? Alternatively, what makes Java/C#/Python etc. easier?
(This question might seem naive, because I might not be familiar with the lower level facilities. I have not messed around with bits/bit shifting or anything like that. I also have not done any GUI/game programming.)
These are some reasons accumulated by the following posts:
- Relationship between arrays and pointers
- Pointer arithmetic
- off by one errors (not C++ exclusive), and no bounds checking
- Templates
- Manual memory management
- code modules (not C++ exclusive)
- Preprocessor
- string support not built in
Some solutions to these problems include:
- Smart pointers for memory management
- RAII for memory management
- iterators for off by one errors
- std::string && std::vector to mitigate the dmg that char* and arr[] cause
Another common complaint seems to be people's insistence on C++ being "C with Classes." I think good programming makes this a moot point, since a good C++ program will avoid procedural programming wherever possible. A good programmer can also avoid the use of the preprocessor where possible (with the exception of module support, where it is a best practice).
Several responses say that C++ is "too large," referring to either built-in facilities or the STL. I think this has less merit because beginners can focus on a small portion of the language to begin with.
Jalf raises an interesting point that C++ is not necessarily hard because of its broad feature range, but because it requires you (there is no option to ignore it) juggle that WHILE learning to actually program. That interpretation is one of the most plausible I've seen yet.
Others so far have raised issue with the unspoken assumption: C++ is harder to learn for beginners. I think it has been pretty hard to learn, but I obviously have nothing else to compare it to. You won't know if your first car handles well until you've been in another. I think it is an understood truth that learning C++ is like learning a manual transmission as your first car. You understand it better later on, but you still end up riding that clutch too much. (It takes courage to ride a metaphor that long, but I did it.)
Common consensus seems to be that once you master C++, mastering any other language is extremely easy (with perhaps the exception of a functional language like F# or Haskell). This seems to be one of the more useful reasons to learn C++ the first time.
Jalf raised some interesting questions about my own knowledge of the language, with this question:
Just for kicks, let's have a few code examples:
int arr[10] = {};
int* p0 = arr + 1; // 1
int* p1 = arr + 10; // 2
int* p2 = arr + 11; // 3
int v0 = arr[11]; // 4
int arr2[5];
int* p3 = arr2 + 2;
int diff = p0 - p3; // 5
float* fp = reinterpret_cast<float*>(p0); // 6
int i = 42;
int j = i++ + ++i; // 7
Which of these lines are legal, and what is the result of them? (Hint: Only three are valid C++, and of those, one of them returns an unspecified value)
My guesses were 1, 5, and 7 were legal. It turns out 1 and 2 are moral and 6 is legal. That was a little bit of a wakeup call as to how intricate my knowledge of pointers are in C++.
Update:
Since this question began as defending my choice of C++ as a first language (and, as of right now, my only language), I figured I should update the people who responded on my progress.
My biggest problem with programming, as of right now, is simple: I don't know how to do anything. Sure, I can write a class that is useful to someone else, but I cannot write anything that would be useful to me in any way. I am not sure, however, if that has anything to do with C++ as the first language - I'm wondering if I'm just not built to be a programmer. It's enjoyable (and extremely frustrating at times), but I'm not sure if I could see myself doing this full time in the future.
One thing I dislike about it is that there are too many solutions to the same problem. This is conducive to several different types of programmers, but I don't feel like I fit the mold of any of them. I think this IS partly C++'s fault, but I could not tell you how I would feel about another language like Python until I've tried it.
I play tennis in my free time, and then solve sudoku if that or socializing are not options. Both of them have multiple ways to solve problems - if my opponent rushes the net, should I lob or try and hit a cross-court shot that he cannot volley? Both methods have easily visible upsides and downsides (the former being, what if it is not high enough or it goes out? The latter being, what if he tags it for a winner on the cross-court?)
The same is not true in programming. One method that looks absolutely fine to me might be hackish, slow, overly complex, or overly simplistic to another programmer. This, coupled with my lack of time/motivation to continue programming, has kept me from doing more than writing a small amount of code each week.
Can anyone think of a way to motivate me past this stage, or do they think I should just give this hobby up?