Learning C++ for C coder



  • I am working in an R&D team where we do mostly scientific programming with a little bit of UI. We've got a new guy in the team and he has never done any C++ (which, you would have guessed, is what we use, otherwise the question would be pointless...). However he already knows C and Fortran, which is quite usual when coming from a scientific programming background.

    So my question is, do you know any good online resources to learn quickly the basics of C++ when you already know C (i.e. without spending time explaining "this is a for() loop, this is a pointer etc.")?

    I'm not looking for complicated stuff, but some simple way to learn about classes, inheritance, templates... I'm not so interested in the standard library because 1) it's mostly just a library so it can be learnt later and 2) we use Qt for a lot of stuff, so not so much the std lib. Also, we mostly have C++98, so the bells and whistles of C++11 are not the most interesting to learn right now.



  • @remi
    I am half tempted to say get some books about OO practices on c#. Forget about the syntax and focus on the code organization.
    Then apply it to c++.


  • Discourse touched me in a no-no place

    @remi said in Learning C++ for C coder:

    So my question is, do you know any good online resources to learn quickly the basics of C++ when you already know C (i.e. without spending time explaining "this is a for() loop, this is a pointer etc.")?

    You might be best with a programming chrestomathy site like Rosetta Code. That will let them compare how they do a task in a language that they already know and how they do it in the language that they are learning.

    It might also be worthwhile putting together your own short guide with examples from your own codebase of practices that are in use. You probably want to start the new guy on just using classes, templates and operators, not creating them; he'll be productive much more rapidly without having to jump down the whole rabbit hole of C++ complexity first…


  • Banned

    The problem with most "C++ for C coder" tutorials is that they teach bad C++, since they focus too much on technical differences and almost not at all on how you should write code. If it were me, I would find some good tutorial on STL's smart pointers (so he understands RAII) and proper inheritance design - and make him learn syntax differences and language features as he goes. Assuming a competent C developer.



  • @dkf said in Learning C++ for C coder:

    You might be best with a programming chrestomathy site like Rosetta Code. That will let them compare how they do a task in a language that they already know and how they do it in the language that they are learning.

    That seems a very good suggestion. Although...:

    It might also be worthwhile putting together your own short guide with examples from your own codebase of practices that are in use. You probably want to start the new guy on just using classes, templates and operators, not creating them; he'll be productive much more rapidly without having to jump down the whole rabbit hole of C++ complexity first…

    That is basically what I was intending as the next step. He won't be creating new classes (well, technically he probably will, but in a very constrained framework where he doesn't have to think about the overall design) initially, mostly using them. I was thinking of pointing him to some external resource so he can learn the basic vocabulary and concepts, and then I will anyway have to walk him through some of our code.

    Not that I would really mind doing that myself, but I think having an external vision of C++, rather than what I would tell him myself, is probably going to be beneficial both to him (there are much better teachers out there than myself!) and to the team (if he can avoid being "corrupted" too quickly by our habits).



  • @Gąska said in Learning C++ for C coder:

    The problem with most "C++ for C coder" tutorials is that they teach bad C++, since they focus too much on technical differences and almost not at all on how you should write code. If it were me, I would find some good tutorial on STL's smart pointers (so he understands RAII) and proper inheritance design - and make him learn syntax differences and language features as he goes. Assuming a competent C developer.

    True, but at the same time, as @dkf guessed, he won't be designing much stuff right now (and, if I was honest, I'd say that our code base probably doesn't have much design overall... but reading the average :wtf: here and some other codebases in the company, I might be too harsh...). So at the moment, I guess the technical differences are probably what matters?

    Also, at some point in the near future he'll probably go to an outside training, where I hope he would learn about more concepts and design principles. But that won't be before at least 1 month and I'd like to find a way to have him do something before that.


  • Banned

    @remi said in Learning C++ for C coder:

    So at the moment, I guess the technical differences are probably what matters?

    Just be careful to not end up with "C with classes".



  • @Gąska said in Learning C++ for C coder:

    @remi said in Learning C++ for C coder:

    So at the moment, I guess the technical differences are probably what matters?

    Just be careful to not end up with "C with classes".

    Yes, except again initially he won't be writing new classes. So in a sense, C with classes might not be so bad a starting point.

    Of course, I'm also thinking longer term and I know that the first contact with something usually forms the overall impression, so if he's initially exposed to purely C with classes, probably no amount of further training will change that. Which is also why I'm looking for the expertise of others: if I were to do a quick session with him, I'd probably fall into that trap...

    I don't really know what I'm looking for, but I guess this discussion is also helping me define that!


  • Banned

    @remi said in Learning C++ for C coder:

    Yes, except again initially he won't be writing new classes. So in a sense, C with classes might not be so bad a starting point.

    Be wary of raw pointers and reinterpret_casting new char arrays.


  • Winner of the 2016 Presidential Election

    @remi
    Just make sure you immediately mention RAII when teaching him about classes, then you should be (mostly) fine. You could teach him about exceptions and exception safety to motivate RAII.

    The other thing you'll have to teach him is that C macros are bad and how C++ templates solve those problems. That might be a bit harder.

    The rest (style, idioms etc.) is something he'll probably learn over time.


  • Discourse touched me in a no-no place

    @asdf said in Learning C++ for C coder:

    The other thing you'll have to teach him is that C macros are bad and how C++ templates solve some of those problems.

    There are capabilities that templates have that macros don't and vice versa too.


  • Banned

    @dkf while there are cases where macro is better than template, usually it's all a bad idea to start with. 🚎



  • @Gąska said in Learning C++ for C coder:

    Be wary of raw pointers and reinterpret_casting new char arrays.

    This is a good point - C-style casts are the devil spawn from hell. If you must cast, use the C++ ones. And if you must cast with reinterpret_cast, ask yourself what you're doing wrong that requires that.

    When I converted from C to C++, I think that was one of the things that I didn't start doing until later... (sigh - I still probably use macros more than I should...)



  • @Gąska said in Learning C++ for C coder:

    @remi said in Learning C++ for C coder:

    Yes, except again initially he won't be writing new classes. So in a sense, C with classes might not be so bad a starting point.

    Be wary of raw pointers and reinterpret_casting new char arrays.

    Hmm, yes, but that's also what code reviews are for :-)

    We are guilty of a number of raw pointers in some parts of our code (some of it being C-code from the 90's that got "ported" to C++... come to think of it, it's even likely Fortran from the 80's or older "ported" to C, "ported"...), so I'd say we are familiar with the associated pitfalls. reinterpret_cast, yeah, seeing one would make me cringe and ask him immediately why he thought that was needed...



  • @asdf said in Learning C++ for C coder:

    @remi
    Just make sure you immediately mention RAII when teaching him about classes, then you should be (mostly) fine. You could teach him about exceptions and exception safety to motivate RAII.

    In a sense, all that is true. But at the minimum to think in terms of RAII, you already need to know what a class (and a constructor/destructor) is, so there some minimal new syntax to learn. Although I guess that thinking with C structs is probably a good starting point to explain the idea.

    And most of our code doesn't use exceptions, so I don't want to spend too much time on them. But again, that's one reason why I want to avoid teaching myself: I've been working so much on that code base that I mentally automatically dismiss exceptions when thinking about it, but while that's (hopefully) not too bad for me, it would probably be a bad idea not to mention them at all when learning C++.

    The other thing you'll have to teach him is that C macros are bad and how C++ templates solve those problems. That might be a bit harder.

    Meh. That doesn't really strike me as a high-priority item. That is true, but that's something that can mostly be learnt as we go along.

    The rest (style, idioms etc.) is something he'll probably learn over time.

    Well, I'm lazy (or: I also have my "normal" work to do as training this guy is not officially part of my duties -- and before you ask whose duty it is: no-one. Yep, management is :doing_it_wrong: but that's business as usual...) so that's why I was hoping for some EZ-C++ :-p



  • @remi said in Learning C++ for C coder:

    Although I guess that thinking with C structs is probably a good starting point to explain the idea.

    But be careful with that... C++ structs are ... different. The idea that a struct is really a class may be mind-blowing! And that you can inherit from them... ooohhhhh.... :)


  • Discourse touched me in a no-no place

    @Gąska I tend to think of both templates and macros as something that is best avoided for someone starting out, and to be minimised in their use even by experts. There's a lot of work that can be done just fine without having to use the most complicated tools possible in the most complicated pattern possible.


  • BINNED

    While all the stuff about smart pointers and templates is all fine in general when talking about C++, I'm wondering how important is it at this point in time given that @remi said they're using Qt?

    As in, if Qt is just to "slap some buttons on the screen", then yes, it's all needed still. But if the whole thing extensively uses Qt's meta-objects and signal/slot architecture... I barely ever touch all that low-level stuff working like that.

    Don't get me wrong, I'm not saying skip the basics, I'm just wondering how important it is to push for that angle right away. But only @remi can decide that I guess, since he's the one who knows the codebase...



  • @dcon said in Learning C++ for C coder:

    But be careful with that... C++ structs are ... different. The idea that a struct is really a class may be mind-blowing! And that you can inherit from them... ooohhhhh.... :)

    I don't use C++ structs. But I think that the point is, you can start with "class (C++) = struct (C) + some new nice features" and talk about the basics. I'm also hoping that, while he may not be familiar with C++ itself, he may already have some OOP concepts.

    @dkf said in Learning C++ for C coder:

    I tend to think of both templates and macros as something that is best avoided for someone starting out

    To start with, I'll probably limit the exposure to templates to the basic idea of std::vector<int>, i.e. a polymorphic container. That should cover the most frequent use, and under this form they are probably one of the most useful part of C++ (compared to C).


  • Discourse touched me in a no-no place

    @remi said in Learning C++ for C coder:

    To start with, I'll probably limit the exposure to templates to the basic idea of std::vector<int>, i.e. a polymorphic container. That should cover the most frequent use, and under this form they are probably one of the most useful part of C++ (compared to C).

    The way to explain them is as being “like an array, but a bit nicer”. Similarly with strings.



  • @Onyx said in Learning C++ for C coder:

    While all the stuff about smart pointers and templates is all fine in general when talking about C++, I'm wondering how important is it at this point in time given that @remi said they're using Qt?

    As in, if Qt is just to "slap some buttons on the screen", then yes, it's all needed still. But if the whole thing extensively uses Qt's meta-objects and signal/slot architecture... I barely ever touch all that low-level stuff working like that.

    I mentioned Qt mostly to justify that we don't use much of the std lib (such as, we use QString rather std::string, QList/QVector rather than std::vector etc.). We also use it heavily for the UI and when it comes to that, I will lead him to the Qt tutorials that are reasonably well done in my experience. But that requires already knowing some C++ (again, the basics about classes, inheritance etc.). The signal/slots part is pretty well explained in Qt's doc.

    But we do use that intensively, yes, so it's probably more important to learn that than, say, exceptions...

    We do have a few bits where we fiddle with the internals, the meta-object and properties, but even between the experienced devs we avoid that as much as possible (I guess looking for places where we do use that is a good sign of where the :wtf: are...) and a new dev will never have to bother with it.

    Don't get me wrong, I'm not saying skip the basics, I'm just wondering how important it is to push for that angle right away. But only @remi can decide that I guess, since he's the one who knows the codebase...

    That is again a very good point, and I guess I'm more and more going away from the idea of pointing him to a website and letting him learn, because that will never be tailored to what we need.

    Still, apart from Rosetta, no one knows of any (half-)good online tutorial?

    (aaaaaaand... thank you 👶 for crashing while I was typing all that!)



  • @dcon said in Learning C++ for C coder:

    @remi said in Learning C++ for C coder:

    Although I guess that thinking with C structs is probably a good starting point to explain the idea.

    But be careful with that... C++ structs are ... different. The idea that a struct is really a class may be mind-blowing! And that you can inherit from them... ooohhhhh.... :)

    It seems like no more of a leap than someone coming from Java/C#/Delphi/etc. land where one is automatically on the stack and the other automatically on the heap.



  • @remi said in Learning C++ for C coder:

    I mentioned Qt mostly to justify that we don't use much of the std lib (such as, we use QString rather std::string, QList/QVector rather than std::vector etc.).

    It amuses me that working with n C++ libraries often means having to deal with n string classes. Fortunately, most of them have some means of converting to raw C strings.



  • @Groaner said in Learning C++ for C coder:

    @remi said in Learning C++ for C coder:

    I mentioned Qt mostly to justify that we don't use much of the std lib (such as, we use QString rather std::string, QList/QVector rather than std::vector etc.).

    It amuses me that working with n C++ libraries often means having to deal with n string classes. Fortunately, most of them have some means of converting to raw C strings.

    Unfortunately, raw C strings cannot contain internal nuls, but if your textual data (which is what strings are supposed to represent) contain nuls, you're probably :doing_it_wrong: anyways, and the data should be treated as binary data (e.g. array of bytes) that just happens to be partly human-readable.

    (Yeah, using myStr.substr(indexAfterNull).c_str() works, but I still contend it's :doing_it_wrong:.)


  • Impossible Mission - B

    You can't learn C++ quickly. I'd even go so far as to say you can't learn it (well) slowly--people with years and years of experience still release code containing newbie mistakes like failing to check for buffer vulnerabilities. (See: every OS ever.)

    In this day and age, learning C++ should be a priori considered :doing_it_wrong:



  • @djls45 said in Learning C++ for C coder:

    @Groaner said in Learning C++ for C coder:

    @remi said in Learning C++ for C coder:

    I mentioned Qt mostly to justify that we don't use much of the std lib (such as, we use QString rather std::string, QList/QVector rather than std::vector etc.).

    It amuses me that working with n C++ libraries often means having to deal with n string classes. Fortunately, most of them have some means of converting to raw C strings.

    Unfortunately, raw C strings cannot contain internal nuls, but if your textual data (which is what strings are supposed to represent) contain nuls, you're probably :doing_it_wrong: anyways, and the data should be treated as binary data (e.g. array of bytes) that just happens to be partly human-readable.

    (Yeah, using myStr.substr(indexAfterNull).c_str() works, but I still contend it's :doing_it_wrong:.)

    I seem to remember one of the Windows file dialogs requiring consecutive ASCIIZ strings terminated by another nul.



  • @masonwheeler said in Learning C++ for C coder:

    In this day and age, learning C++ should be a priori considered :doing_it_wrong:

    What should game developers do, then, if they don't like Unity?



  • @Groaner said in Learning C++ for C coder:

    I seem to remember one of the Windows file dialogs requiring consecutive ASCIIZ strings terminated by another nul.

    Writing a REG_MULTI_SZ to the registry also requires that... (tho it can be unicode too) And don't forget to include the extra null in the length of the buffer passed in!


  • Discourse touched me in a no-no place

    @Groaner said in Learning C++ for C coder:

    What should game developers do, then, if they don't like Unity?

    There's always asm.js…


  • Winner of the 2016 Presidential Election

    @masonwheeler said in Learning C++ for C coder:

    In this day and age, learning C++ should be a priori considered :doing_it_wrong:

    C++ is at least getting better, and can be quite nice if you have a modern compiler and use it correctly. But plain C needs to finally die. Preferably in a huge fire.



  • @asdf said in Learning C++ for C coder:

    @masonwheeler said in Learning C++ for C coder:

    In this day and age, learning C++ should be a priori considered :doing_it_wrong:

    C++ is at least getting better, and can be quite nice if you have a modern compiler and use it correctly. But plain C needs to finally die. Preferably in a huge fire.

    Samsung's trying.


  • Discourse touched me in a no-no place

    @asdf said in Learning C++ for C coder:

    But plain C needs to finally die.

    Did they manage to make C++ easy to make stable ABIs with yet? No? Or make the linking of the C++ standard library less obnoxious? I think C will be around a long while yet…



  • @masonwheeler said in Learning C++ for C coder:

    In this day and age, learning C++ should be a priori considered :doing_it_wrong:

    Yeah well, thanks for the totally useless comment. I am sure that telling that to my manager will make him change his mind immediately and that we will throw away a few millions lines of code, rather than training someone.

    Sarcasm apart, when you're not working in an isolated environment or small projects where you can rewrite everything at will, how do you deal with a new guy joining an established team?


  • Winner of the 2016 Presidential Election

    @dkf said in Learning C++ for C coder:

    Did they manage to make C++ easy to make stable ABIs with yet?

    Let's hope this will finally be solved with the next version of the C++ standard after C++17.


  • Discourse touched me in a no-no place

    @asdf said in Learning C++ for C coder:

    Let's hope this will finally be solved with the next version of the C++ standard after C++17.

    Ah. So that's a “not solved yet” then…


Log in to reply