Imho, C++ should be thought of as a new language with a degree of backward compatibility with old C code, not as a 'bigger C'.
How I'd start:
Lesson 1: Pointers don't exist in C++, we
have references.
Lesson 2: malloc() doesn't exist in C++
Lesson 3: Templates are awesome.
Lesson 4: Smart pointers are awesome.
Lesson 5: 'Structs' can have constructors, and
they're awesome!
Lesson 5.5: Exceptions are awesome!
Lesson 6: 'Structs' can have copy constructors and
assignment operators, but the default ones do
exactly what you'd expect so you shouldn't have
to write them often.
Lesson 7: OK, OK... pointers still exist in C++ but
smart pointers and references make them almost
deprecated, right?
Lesson 8: ...oh, and if you use raw C pointers you
need to do all this extra donkey work when it
comes to handling exceptions, and write a lot
more of those pesky copy and move constructors
and assignment operators I told you you didn't
need to write.
Example: writing a smart pointer (with C pointers!)
Lesson 9: Implementing a safe, dynamic, exception-safe
array in C++ using templates and C pointers.
Lesson 10: back to sanity, the ease of using <vector>
(or the output of lesson 9) in every day code.
Introduction to the STL, <string>, etc.
...and so on. The idea is to first teach the common C++ way, and then teach the penalty of ignoring it and going the C way, and then the places where you need to pay that penalty.
Imho teaching inheritance or virtual functions early is also a bad sign. I'd go with public/private access control first (leaving protected), then member functions, then pure virtual member functions and basic inheritance, all the while presenting no hint of Circles and Ellipses, just interfaces.
This is the approach taken by Bjarne in his new book "Tour of C++".
I tend to bash C and C++ a lot given their unsafe by default nature as a Pascal refugee, but at least C++ can be made safe when the right abstractions are used and I do like it anyway.
I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library.
I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and would love for an expert to teach me.
In general, I wonder whether there's really that many people who know C but not C++. This might just be me projecting myself onto the world, though. Anyone?
Use GObject (https://developer.gnome.org/gobject/stable/) and GLib. GObject is basically a bunch of macros to let you write OO code in C, GLib is a cross-platform set of libraries with somewhat of a 'standard library' of data structures, string handling stuff etc. It's the foundation of Gnome, it's also LGPL so that may be an issue. To be honest though I've never used it for strictly C-only code, I've only used it on Linux with gcc and it was 10 years ago.
Still, it's a nice hack for when you want to write C++-like code while still using C constructs only.
I've done so little C++ so long ago that I may as well fall in the category of a C programmer who doesn't know C++. If I had spare time over the next 8 weeks I'd definitely do this as a refresher course.
That statement is true. But, as you may be aware, it doesn't encompass quite a few use-cases. A lot of usecases require you to have fine control over performance and using RAII will cause random speed-bumps. for e.g. when a ref-counted smart pointer(s) goes out of scope and triggers 'heavy' destructors on held objects to free up resources - at a non-deterministic time.
I use C++ because of the std containers (vector, map, set, etc). I don't have to roll my own and I'm free to think about the problem at hand rather than how to get the proper data container for this, that and the other.
I can understand this - I know some C++ but I am not very good at it, so I usually go for C with "greenspunning", i.e. using an embedded scripting engine, e.g. guile/lua/spidermonkey
Forget about the stories. I have quite some experience with both C and C++, started with C. There's horror in both of them, if you do it wrong enough. Oh yes there's more horror in C++, there are more ways to do it wrong. But in the end it comes down to the same: learning curve. If you know it well enough there's no horror anymore, just joy.
If you need C++ for a living then you have no other option.
If you want to enjoy programming then I recommend to stay away from C++. I have written a lot of software in C and C++ in my programming career but C++ is not fun anymore. There are so many (even free) implementations of better languages. For instance C#, Python, Go.
If you want to learn a next generation language right now then you should look at Rust. It seriously has the ability to replace C++ in the long term. C++ will be remembered as the third dino besides Fortran and Cobol.
That is exactly right... It is like they are assuming that by knowing C, then implicitly they know C++. That's tremendously wrong and they will notice that as soon as they work on a non-so-small project.
OK I'm going to date myself here...when I studied comsci/CPrE in college, we started out with C and moved to C++ and then to Java. I guess it doesn't speak much for the depth of the classes that I found C++ pretty easy to get into and used it in higher level courses, such as one involving Open GL.
I guess what I'm asking is...who uses just C any more? I can only guess that if, by this point, your main expertise is C and you are still hacking away at it, you're a hacker who is beyond the need for any introductory C++ course and who could probably learn it on your own. If you're a C-person and just want to dabble in a more OOP language to do fun things, why wouldn't you just jump into Python? The world of mostly-and-only-C-programmers seems to be fairly small these days, and the ones who need to get into C++ -- in lieu of what's out there --- seems even smaller
To reiterate my thoughts, here's a review from someone who has read the recommended text book:
> I was initially dismayed by the large number of negative reviews, but now that I've completed this book I believe I understand why; this book is really intended for a very narrow target audience: very experienced C programmers with no knowledge of C++ -- i.e., Old Farts,... like me. Is there ANY school out there today teaching people C WITHOUT also teaching them C++? It seems unlikely, almost as unlikely as it would be to find a potential employer out there who is seeking Plain Old C programmers today. So if you are an Old C Dog like me looking to learn the "new" trick of C++, this book is for you and ONLY for you! I read it carefully and was able to pass a C++ test that I wouldn't have gotten a single answer right on before I read it. Anyone else looking to learn C++ would be better served by choosing C++ for Dummies or something similar.
There are many, many people who use C and rarely or never use C++. Most embedded work is done in C, the Linux and BSD kernels are in C, etc. There are a variety of reasons for this, with audit-ability and debug-ability being two big ones. Code space is also a reason, although there are ways to make C++ mostly behave the way you want it to on all but the teensy tiniest platforms (although the resulting code is sometimes a questionable upgrade over just writing the thing in C). A great number of language interpreters are also written in C rather than C++ (cpython being one of them) - easier bindings are one reason for this.
As another datum, where I went to school the intro to CS data structures and algorithms courses were taught in C++ and the operating systems course was taught in C. In my day to day work I use C and a tiny bit of assembly (more reading than writing).
[+] [-] nly|12 years ago|reply
How I'd start:
...and so on. The idea is to first teach the common C++ way, and then teach the penalty of ignoring it and going the C way, and then the places where you need to pay that penalty.Imho teaching inheritance or virtual functions early is also a bad sign. I'd go with public/private access control first (leaving protected), then member functions, then pure virtual member functions and basic inheritance, all the while presenting no hint of Circles and Ellipses, just interfaces.
[+] [-] pjmlp|12 years ago|reply
I tend to bash C and C++ a lot given their unsafe by default nature as a Pascal refugee, but at least C++ can be made safe when the right abstractions are used and I do like it anyway.
[+] [-] ErsatzVerkehr|12 years ago|reply
http://www.amazon.com/For-Programmers-Third-Edition-3rd/prod...
[+] [-] checker659|12 years ago|reply
[+] [-] skrebbel|12 years ago|reply
I'm pretty well versed in C++, and every time I try to write something in C, I fall back to C++. I find memory management a pain without RAII, error handling a hassle without exceptions, and, well, anything really a pain without some sort of standard container library.
I have the idea that there's good alternatives for these in C-land, but I'm unfamiliar with them and would love for an expert to teach me.
In general, I wonder whether there's really that many people who know C but not C++. This might just be me projecting myself onto the world, though. Anyone?
[+] [-] luikore|12 years ago|reply
In C11 there is type generic macros that helps writing C++-like overloaded functions.
There are several extensions in GCC too.
For RAII-like memory management, there are __attribute__((constructor)) and __attribute__((destructor)).
C++ does type reflection on constant expressions via type traits templates, in GCC there is simply typeof().
If you are bored about writing static function modifiers, you can write a function inside function.
If you want to allocate a vector which won't change capacity and be released when out of local scope , for example in C++:
With C99 dynamic arrays you can use this instead: You can use the union trick to achieve safer type casts too.[+] [-] roel_v|12 years ago|reply
Still, it's a nice hack for when you want to write C++-like code while still using C constructs only.
[+] [-] yoodenvranx|12 years ago|reply
http://c.learncodethehardway.org/book/ex20.html
[+] [-] wrl|12 years ago|reply
[+] [-] paulbarker|12 years ago|reply
[+] [-] ksk|12 years ago|reply
That statement is true. But, as you may be aware, it doesn't encompass quite a few use-cases. A lot of usecases require you to have fine control over performance and using RAII will cause random speed-bumps. for e.g. when a ref-counted smart pointer(s) goes out of scope and triggers 'heavy' destructors on held objects to free up resources - at a non-deterministic time.
[+] [-] amboar|12 years ago|reply
[+] [-] udp|12 years ago|reply
It's certainly not standard, but I wrote a nice little generic list library you might be interested in: http://github.com/udp/list
It uses GNU extensions, but can fallback on C++ instead if you need to build with MSVC.
[+] [-] 16s|12 years ago|reply
[+] [-] finnw|12 years ago|reply
[+] [-] CraigJPerry|12 years ago|reply
AIUI exceptions and RAII are mutually exclusive in C++. I don't know enough about C++ so i wouldn't be surprised to find out this is wrong.
[+] [-] dysoco|12 years ago|reply
In general, anything that says "X for Y programmers" is not worth it.
If you really want to learn C++, and you already know C, I suggest either "Accelerated C++" or "The C++ Programming language"
[+] [-] 16s|12 years ago|reply
[+] [-] PLejeck|12 years ago|reply
[+] [-] stinos|12 years ago|reply
[+] [-] progman|12 years ago|reply
If you want to learn a next generation language right now then you should look at Rust. It seriously has the ability to replace C++ in the long term. C++ will be remembered as the third dino besides Fortran and Cobol.
http://www.rust-lang.org
[+] [-] _pmf_|12 years ago|reply
[+] [-] hconceic-HN|12 years ago|reply
[+] [-] kbart|12 years ago|reply
[+] [-] checker659|12 years ago|reply
[+] [-] guard-of-terra|12 years ago|reply
RUN
[+] [-] antimagic|12 years ago|reply
[+] [-] checker659|12 years ago|reply
[+] [-] danso|12 years ago|reply
I guess what I'm asking is...who uses just C any more? I can only guess that if, by this point, your main expertise is C and you are still hacking away at it, you're a hacker who is beyond the need for any introductory C++ course and who could probably learn it on your own. If you're a C-person and just want to dabble in a more OOP language to do fun things, why wouldn't you just jump into Python? The world of mostly-and-only-C-programmers seems to be fairly small these days, and the ones who need to get into C++ -- in lieu of what's out there --- seems even smaller
To reiterate my thoughts, here's a review from someone who has read the recommended text book:
http://www.amazon.com/For-Programmers-Third-Edition-3rd/prod...
> I was initially dismayed by the large number of negative reviews, but now that I've completed this book I believe I understand why; this book is really intended for a very narrow target audience: very experienced C programmers with no knowledge of C++ -- i.e., Old Farts,... like me. Is there ANY school out there today teaching people C WITHOUT also teaching them C++? It seems unlikely, almost as unlikely as it would be to find a potential employer out there who is seeking Plain Old C programmers today. So if you are an Old C Dog like me looking to learn the "new" trick of C++, this book is for you and ONLY for you! I read it carefully and was able to pass a C++ test that I wouldn't have gotten a single answer right on before I read it. Anyone else looking to learn C++ would be better served by choosing C++ for Dummies or something similar.
[+] [-] KaeseEs|12 years ago|reply
As another datum, where I went to school the intro to CS data structures and algorithms courses were taught in C++ and the operating systems course was taught in C. In my day to day work I use C and a tiny bit of assembly (more reading than writing).
[+] [-] otikik|12 years ago|reply
[+] [-] anon1385|12 years ago|reply
One wonders how people who can't even create a static HTML page will manage to teach people a complex language like C++.
[+] [-] jmarin|12 years ago|reply
[+] [-] timdiggerm|12 years ago|reply