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++?

yati sagade
  • 2,089
  • 2
  • 19
  • 27
  • 9
    Based on your comments you know the C++ syntax and that is all. You are not coding in C++. The [C++ tag on stackoverflow](http://stackoverflow.com/tags/c%2b%2b/info) is a good place to start, it includes a [reading list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and [FAQ](http://stackoverflow.com/questions/tagged/c%2b%2b-faq). The only real way to learn is to write code and get experienced user to comment. You can put your code [here](http://codereview.stackexchange.com/questions) for review. [A good example](http://codereview.stackexchange.com/q/3714/507) – Martin York Nov 03 '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++. – Jerry Coffin Nov 03 '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 :) – yati sagade Nov 03 '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 03 '11 at 20:45
  • @LokiAstari I think you should convert your comment to an answer so we can vote you up... – yannis Nov 03 '11 at 20:46
  • Possible duplicate: http://programmers.stackexchange.com/questions/48401/learning-c-properly-not-c-with-classes – yannis Nov 03 '11 at 20:47
  • @YannisRizos apprehended such a comment/answer and here it is :) I feel like swearing upon the professor :\ Nevertheless, thanks! :) – yati sagade Nov 03 '11 at 20:49
  • @LokiAstari I second Yannis – yati sagade Nov 03 '11 at 20:53
  • 2
    *I'm all comfortable with [...] - (int)(someVoidPointer)* Ever debugged a 64-bit build before? – Ed S. Nov 03 '11 at 20:55
  • @Ed NO. I am comfortable with *that* (fairly common) idiom in C (atleast on 32 bits) - That's all I said. I don't advocate/evangelize the practice. – yati sagade Nov 03 '11 at 20:58
  • The good thing is that it's easier to go from "C with classes" to "Object-oriented C++" than the other way around. Be thankful you're not a Java programmer who can't grasp the concept of a pointers (no offense to Java programmers - they're just different things to know). – Pubby Nov 03 '11 at 21:12

7 Answers7

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 :-)

Martin York
  • 11,150
  • 2
  • 42
  • 70
  • This is not the only way. You can learn a lot from good books. – Dima Nov 03 '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. – Martin York Nov 03 '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 03 '11 at 21:14
  • @Dima: I have no argument with that. – Martin York Nov 03 '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.

Pubby
  • 3,290
  • 1
  • 21
  • 26
  • I know about the effective series, (haven't read them) - But I guess they're for best practice issues, right? – yati sagade Nov 03 '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 03 '11 at 21:02
  • I see - actually I had effective c++ by Meyers lined up after Accelerated C++. – yati sagade Nov 03 '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 03 '11 at 21:07
  • @Pubby8: and this fits the poster's situation perfectly. – Dima Nov 03 '11 at 21:10
  • Whoops, I meant to direct that comment to @yatisagade. I also like how Meyers refers to C++ as a "federation of languages" – Pubby Nov 03 '11 at 21:14
  • haha "federation" it is!! C++ can compete with Haskell in giving you headaches (sometimes) ;) Thanks a ton! – yati sagade Nov 03 '11 at 21:31
  • 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 04 '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.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • +1 Thanks. That was wonderful. I'm happy I asked this question. So many wonderful links :) – yati sagade Nov 04 '11 at 18:18
  • 1
    Link is dead, I found a copy here: https://www.youtube.com/watch?v=Kghns7c8Ij8. Although since this talk is from 2011, it might make sense to look for a more recent one – Cerno Jan 22 '23 at 09:17
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.

sbi
  • 9,992
  • 6
  • 37
  • 56
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.

einpoklum
  • 2,478
  • 1
  • 13
  • 30
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).

littleadv
  • 4,704
  • 27
  • 26
-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.

Coder
  • 6,958
  • 5
  • 37
  • 49
  • "most of the C++ code is wrong" - can you clarify? I've seen that most OSS projects actually maintain high quality standards. – yati sagade Nov 03 '11 at 21:09
  • 1
    Projects, yes, but googling on how to do stuff is quite often leading to bad examples. – Coder Nov 03 '11 at 22:05
  • 1
    @yati: My experience re OSS projects is the opposite. – sbi Nov 04 '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! – yati sagade Nov 04 '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
  • @yatisagade: About Google Chrome and Mozilla - I don't know about their discipline, but a lot of the Mozilla code (not saying the majority mind you) is rather atrocious. And - almost all of it is _quite_ inappropriate as modern C++. – einpoklum Mar 24 '18 at 22:40