31

I am a computer science student, and as a result, I was taught C++ as a better version of C with classes. I end up trying to reinvent the wheel whenever a solution to a complex problem is needed, only to find sometime after that, some language feature or some standard library routine could potentially have done that for me.

I'm all comfortable with my char* and *(int*)(someVoidPointer) idioms, but recently, after making a (minor) contribution to an open-source project, I feel that is not how one's supposed to think when writing C++ code. It's much different than C is.

Considering that I know objected-oriented programming fairly well, and I am okay with a steep learning curve, what would you suggest for me to get my mind on the C++ track when I'm coding C++?

11
  • 7
    Based on your comments you know the C++ syntax and that is all. You are not coding in C++. The C++ tag on stackoverflow is a good place to start, it includes a reading list and FAQ. The only real way to learn is to write code and get experienced user to comment. You can put your code here for review. A good example Nov 3 '11 at 20:19
  • 1
    Along with @LokiAstari's advice (with which I agree), I'd say your friends are right, and working your way through Accelerated C++ would probably be a good idea. I suspect you'll find that a lot less skimming is needed that you expect -- it's intended for people in your position, already knowing programming, and primarily needing to learn the idioms of modern C++. Nov 3 '11 at 20:27
  • yes actually I completed the first two chapters, but the stuff there was mostly what I already knew - I understand an author won't just come out and write a book for me, though :) @LokiAstari thanks for the superinformative comment :) Nov 3 '11 at 20:31
  • 3
    @yatisagade Don't expect a book to teach you the language's mentality in a couple of chapters. It may be extremely boring, as you know the syntax already, but you should commit yourself to going through a couple of C++ books, reading everything and doing each exercise as you were a total beginner to the language. Being taught the wrong way means you have to double your efforts, as you need to forget the wrongs and learn the rights (or something like that).
    – yannis
    Nov 3 '11 at 20:45
  • 1
    I'm all comfortable with [...] - (int)(someVoidPointer) Ever debugged a 64-bit build before?
    – Ed S.
    Nov 3 '11 at 20:55
14

Based on your comments you know the C++ syntax.
You are not coding in C++ but what is often refereed to as C with classes.

The C++ tag on stackoverflow is a good place to start, it includes a reading list and FAQ.

The only real way to learn is to write code and get experienced user to comment. You can put your code here for review. A good example

I'm all comfortable with my "char*" s

Stop using them, switch to std::string.

and (int)(someVoidPointer) idioms.

Stop using them (apart from to interface with C code). Using the functor concept provides several advantages (included the idea of encapsulating state).

But recently, after making a (minor) contribution to an OSS project, I feel that is not how you think in C++. It's much different, though C has its own place.

Yes. C and C++ have diverged as languages. Though you can use practically the same syntax what is considered good C code is generally not considered good C++ code (or vice verse).

Some friends have suggested Accelerated C++, but again I know what types are, and what classes are and what overloading is.

You have the very basics down.

How can a (mutilated) C++ programmer, who happens to be sound with the OO concepts write idiomatic programs in the language.

With a lot of work :-)

4
  • This is not the only way. You can learn a lot from good books.
    – Dima
    Nov 3 '11 at 21:05
  • 1
    @Dima: Absolutely. You can learn a lot from books. But nothing will beat experience and using the language in anger, failing re-trying failing again and work out the best way to do it. I suppose you can learn French from a book but I doubt the French would consider you fluent. Nov 3 '11 at 21:08
  • 6
    IMHO, the most efficient way to learn is to start with the books, learn the right way to do things, try it in practice, and then have somebody critique your code.
    – Dima
    Nov 3 '11 at 21:14
  • @Dima: I have no argument with that. Nov 3 '11 at 21:28
13

The book Effective C++ teaches a number of interesting things and will bring you to appreciate the features of C++. There is also Effective STL - I have not read it but I'm sure it would be a great read if you are unfamiliar with the STD.

The important thing to learn is that you should make use of the language and don't reinvent the wheel constantly. You've already learned how to make them, so make it easy on yourself (and others!) and use tools to all their potential.

As a side note, you'll run into a lot of people who demand the STD be used. This is just as bad of mindset as only using char* - sometimes it's not the correct tool and there are many other out there. In the same sense, don't be discouraged from making your own container classes - if you're going to be using char* the best place to do it is safely wrapped inside a class.

8
  • I know about the effective series, (haven't read them) - But I guess they're for best practice issues, right? Nov 3 '11 at 20:56
  • 3
    @yati sagade: no, those books are exactly what you need to go from "C with classes" to full-fledged C++.
    – Dima
    Nov 3 '11 at 21:02
  • I see - actually I had effective c++ by Meyers lined up after Accelerated C++. Nov 3 '11 at 21:04
  • 1
    @Dima "The purpose of this book is to show you how to use C++ effectively. I assume you already know C++ as a language and that you have some experience in its use. What I provide here is a guide to using the language so that your software is comprehensible, maintainable, portable, extensible, efficient, and likely to behave as you expect." - Effective C++. It also covers a number of "gotchas" which make it interesting to read.
    – Pubby
    Nov 3 '11 at 21:07
  • 1
    @Dima: Careful here. The 2nd edition was aimed at new C++ programmers coming from C. The (latest) 3rd edition is more aimed at programmers coming from languages like Java, C# etc.
    – sbi
    Nov 4 '11 at 13:15
7

I can recommend the recent BUILD take given by Herb Sutter. The one called "Writing modern C++ code: how C++ has evolved over the years":

Many people think of C++ as the same language they experienced in college or just as “C with classes”, but the C++ language has evolved extensively over the years. In this session, we’ll cover how you can use C++ to write innovative, expressive and beautiful apps that deliver power and performance apps. Join us to see how the newly finished C++0x standard can make writing C++ as productive as many other languages.

Its not a bad presentation, not too long, has some nice pointers to the new features in the latest standard which would give you a few hints towards getting your old C/C++ style updated.

Other than that, you need to learn the STL - its not complicated and there are plenty of books, eg, Effective STL, or just google for STL tutorials to get you going.

1
  • +1 Thanks. That was wonderful. I'm happy I asked this question. So many wonderful links :) Nov 4 '11 at 18:18
6

I read Accelerated C++ by Andrew Koenig and Barbara Moo in order to help me teaching C++, after having worked with C++ for nearly a decade. (In fact, I was starting to tinker with template meta-programming at that time.) I still found it to be a revelation, even though I don't think it taught me any new fact about the language.1

What it taught me, though, was to look at, and use, C++ as a high-level language. To not to tinker with raw pointers and delete and to use the standard library wherever possible.

I have the feeling that this is exactly what you are after.

1 Not that there wasn't anything left to teach me back then (there is plenty even now, a decade later), but there's only so much knowledge you can squeeze into a 250 pages introductory book.

2

Before answering - a note: Idiomatic C++ is a moving target. As the language changes, so do its idioms. In fact, some language features are intended to allow us to do away with idiomatic code which might be simplified or improved with some support from the language itself, or at least the standard library. So bear in mind that any source can only inform you on what was idiomatic at the time of writing.

Having said that, you would do well to check out:

The codereview.SX site

The StackExchange network has a site named codereview.stackexchange.com. If you've written a piece of C++ code - a class, a part of a library, something not overly huge - you can post it there and ask the community to review it. Note that the code has to compile and be basically-functional - that site is not for debugging.

Also, you could search for some C++ code there, related to what you're working on, and see what reviewers emphasize. You could even try reviewing the posted code yourself without posting an answer, then reading what others thought of it.

C++ conference video presentations

There are several developer conferences focused on C++ held every year:

and those are not even the only ones... anyway, each of these posts videos of many/all of the presentations. These will teach you a lot, including some idiomatic coding, and principle with which you can decide what should or shouldn't be idiomatic.

Sometimes you can even get the speakers' slide decks.

0

Well, open source projects is a very good start. Don't expect to become a professional programmer based on your university classes, they are not intended for that (as I wrote in my answer here).

You seem to be aware of the syntax, that's good. Now go and read others' code, and contribute your own for peer reviews. You'll learn a lot from reading code and trying understanding it, debugging it and fixing it, and of course - adding to it adhering the coding conventions that are already there.

Professional books are also a very good idea, as mentioned, and browsing through the questions and answers on StackOverflow will teach you a lot (it surely teaches me a lot, and I consider myself a C++ professional).

0
-1

Reading a lot of good C++ code will probably help. You have to see the code to start getting a feel of it. Then again, most of the C++ code is wrong.

6
  • "most of the C++ code is wrong" - can you clarify? I've seen that most OSS projects actually maintain high quality standards. Nov 3 '11 at 21:09
  • 1
    Projects, yes, but googling on how to do stuff is quite often leading to bad examples.
    – Coder
    Nov 3 '11 at 22:05
  • 1
    @yati: My experience re OSS projects is the opposite.
    – sbi
    Nov 4 '11 at 13:15
  • @sbi maybe - I was involved with the Mozilla project and Google Chrome(not involved here actually, but kind of learning). Found their discipline outstanding! Nov 4 '11 at 18:37
  • @yatisagade: At the very least, most C++ code in existence was written when the language did have features which are today essential to writing good code; and most of the rest was written having to use libraries which were written to work with such older code, so this newer code must be adapted to them, to some extent. And all this is regardless of whether coders invest the time and effort necessary for decent design and implementation...
    – einpoklum
    Mar 24 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.