I’m no stranger learning new languages, having learned 4* of completely different paradigms in half a year, as well as several frameworks, but I’m having trouble with C++. I know the answer is “do something in it” 99% of the time, but I have done stuff in it and I just can’t “get it.”
Oh sure, the programs compile and run, but it all basically devolves into C code, but with a couple alleviating factors. Is there any good way to overcome my “C-ness”? I’m no stranger to OO programming, if I had to choose my most “fluent” language it would be Java (what we’re taught in my college as the learning language of choice).
Is there any guideline for an “acceptable” level of “C-ness” (though I suppose that depends on what you’re writing, exactly). Is there any way to really think about when to use some things? Are there any “programmer courtesy” conventions that change from C conventions?
Honestly, I’d prefer to just forget C and C++ altogether, sure, there’s performance, but half the security holes in the world’s programs stem from it. Alas, half in-use frameworks in the world also use C++, and most of the jobs around here want you to be experienced in C++.
No, C++ is a horrible Frankenstein hybrid. There is no good way to learn it. You know the syntax and you understand OO, and still you fall back into straight C procedural programing. That should tell you something.
From what I’ve seen, most projects take advantage of the C++ constructs to design a top-down object-oriented program and then all of the methods are written mostly with only C conventions, excepting (snerk) the I/O.
A class is just a struct whose member data defaults to private instead of public. You really can’t do anything C+±specific until you start dealing with virtual functions and polymorphic variables, and if you don’t need that, why worry about what language you’re working in?
The problem I see with C++ is that there is no “right” way to do things – there are usually 3 or 4 different “right” ways to do it as it implements everything C does, plus OO, plus multiple inheritance, plus templates, plus different libraries which handle things like strings in several different ways depending what you decide to use. It’s the kitchen sink language where you can do almost anything in it, but there are really no “best practices” as the language is so huge, you can never learn more than a subset of the entire language set. Yep, you can write code in it, but you always feel like there is a better way and often you are correct. But do we want to go learn complex template expansion today or do we want to get work done? At some point you have to roll with what you know to get something done.
I think the big point is that there’s no need to use the entire language. C++ has an awful lot of very complex language features (most of them, it seems to me, involve templates) but the bulk of most C++ code that I’ve ever seen has been straight C.
FWIW, when coding in C++ I use references all the time (because they simplify the hell out of pointers, especially when passing parameters). I use try blocks occasionally but I usually attempt to code without them first. I use parts of the standard library (e.g strings, vectors, etc) quite frequently, but not invariably. I almost never use streams for I/O because C I/O is much simpler.
I’ve written simple templates but template coding (as opposed to template using) can look nightmarish to me.
I’m quite looking forward to compilers that support the '11 standard, if for no other reason than so I can use “auto” to declare variables with automatic types.
Thanks, it turns out the Science and Engineering library at my University had it (also, I officially find the Library of Congress system mildly annoying), and since I’m an honors student I can check things out for an absurd 6 months with no renewal so I have time to slowly look over it. I’ve read the first chapter or so and it does seem like a good book so far.
It’s good to know that a lot of C++ devolves into C anyway. I had to work in C++ a couple days ago for a graphics course, and while I only really had to alter one method, I did realize that aside from the odd “new” here and there, it basically was C code, save for the one header file that defined templates and classes.
Once you “get” objects, you’ll abandon C++ for an actual object-oriented language.
I think C++ ought to be used as Stroustrup intended — take advantage of some of the organization OO provides without going so far as to make it slow. Which basically means ignoring any templates. That’s not to say that some jobs do require the use of C++ when it makes almost no sense for them to use it.
I’ve near-abandoned my old desire to work as a programmer for video game companies (would prefer to get a doctorate in AI/Machine Learning instead), but it seems like the vast majority still use C++. As far as I can tell, it’s inertia, plain and simple*. All the fancy schmancy pre-boxed engines and libraries for games are in C++, and they recycle code from their old games to reduce production times which, surprise! Are in C++. There has been some breakthrough in C-lua, where the underlying, performance intensive architecture can be in C or C++, but the rest is in LUA, but it doesn’t solve the problem of knowing C++, since they just expect you to know both now.
The problem is that I get objects just fine, I just know C as well, so C++ specifically devolves into C, and I save my OO-ness for other languages (unintentionally).
Well, and possibly performance concerns, there’s a lot of bickering wars on whether or not C++ is really necessary, performance-wise, for games nowadays.