Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
4  
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 –  Loki Astari 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++. –  Jerry Coffin 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 :) –  yati sagade Nov 3 '11 at 20:31
2  
@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 Rizos 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
show 6 more comments

6 Answers

up vote 10 down vote accepted

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

share|improve this answer
    
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. –  Loki Astari Nov 3 '11 at 21:08
3  
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. –  Loki Astari Nov 3 '11 at 21:28
add comment

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.

share|improve this answer
    
I know about the effective series, (haven't read them) - But I guess they're for best practice issues, right? –  yati sagade 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++. –  yati sagade 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
show 3 more comments

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.

share|improve this answer
    
+1 Thanks. That was wonderful. I'm happy I asked this question. So many wonderful links :) –  yati sagade Nov 4 '11 at 18:18
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
+1 indeed SO teaches us all a lot :) –  yati sagade Nov 3 '11 at 20:59
add comment

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.

share|improve this answer
    
"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 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! –  yati sagade Nov 4 '11 at 18:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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