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 know C# pretty well (self-taught, sadly) and need to do some C++ programming for a Windows application. I have been able to find a ton of information for C++ developers learning C# but haven't been able to find much on learning C++ when you already know C#. Has anyone come across a good rundown of the basics?

MSDN has a comparison but it is not very in-depth.

I can piece together several sources but figured something was out there - I just can't find it. Thanks for your help.

share|improve this question
14  
Why "sadly"? Don't put yourself down like that. I don't know a C# developer that isn't self-taught. Many of which are amazing developers. –  Steve Evers Jan 2 '12 at 21:52
4  
@SnOrfus -- mainly because academia shun it for irrational pseudo-political reasons. –  Rei Miyasaka Jan 3 '12 at 6:18
3  
My first word of advice is don't try to learn C++ in the context of C#. Because C# is not C++ and C++ is not C#. That might be a stupid statement to make but C# makes it hard to do pointers wrong, and its very easy to do them wrong in C++, even with the tools that make it realtively easy. I would add there are many things that the two languages share in common. It basically is like saying an Apple and Orange are both grown on a tree and have seeds and thus they are fruits. –  Ramhound Jan 4 '12 at 15:08
2  
@ReiMiyasaka, not unlike how C# developers look at VB .NET. –  Kyralessa Jan 4 '12 at 22:06
    
@Kyralessa Which is also kind of unfortunate. I think it's not so much a political issue as it is condescension towards the VB.NET demographic -- because most of the C# early adopters have a C/C++ background. My school used VB.NET for beginner courses though. They seem to think that VB.NET is a toy, and so shouldn't be as corrupting to the minds of students as C# is. –  Rei Miyasaka Jan 4 '12 at 23:19
show 3 more comments

9 Answers

up vote 6 down vote accepted

This thread on C++ for Java developers is very similar and should be useful, especially Mark Byer's post:

Read these books:

Probably the most important thing to keep in mind is to understand the difference in memory management techniques between C#/Java and C++.

One of my profs came from a Java background and taught an introductory C++ course thinking it would be cake. Nothing he explained made any sense to anyone. He had had memory leaks everywhere. His pointer arithmetic was terrible. He never got to the OOP part of the syllabus (thank god). Virtually nothing he demonstrated would have compiled. He didn't understand that char[] needed a sentinel \0. Don't end up like him.

share|improve this answer
2  
Pity that nobody in C++ uses char[] then, isn't it? –  DeadMG Jan 4 '12 at 20:22
3  
@DeadMG I can't quite see your point. Just because char[] is seldom used in C++ doesn't mean it's not taught or that it shouldn't be taught. char[] is still important to be aware of because C++ is a superset of C, and it's imperative that it's understood correctly if it's to be learned at all. –  Rei Miyasaka Jan 4 '12 at 23:09
4  
Yeah- a very very, very long time after they have mastered about every other aspect of the language. I'd rather teach new students partial template specializations and SFINAE. In C++, you use std::string, and if you have to interop with a C API, then that's what c_str() is for. There's no need for the programmer to deal with char[] or NULL termination in any of that. The C-style string handling is completely and utterly and totally deprecated for every possible use except interop with old C code, which is perfectly well handled by c_str(). –  DeadMG Jan 5 '12 at 0:42
2  
There is no such thing as teaching char[] right, because all uses of char[] are wrong. Your professor would probably have been absolutely fine if he'd stuck to C++ instead of C. –  DeadMG Jan 5 '12 at 12:22
3  
You will see C code used in C++ whether you write C or not yourself, and consequentially, if you're learning C++, you need to learn C. As I've said, I don't condone it, but the fact of the matter is that schools generally teach C and then tack on C++ later, because students need to be taught things like OS design -- which, excluding BeOS and certain research OSes, absolutely require C. For that, you do need to understand C strings. Again, for the third time, if I had my way, I would not teach C++ at all to first year students, and for the last time, that's completely besides the point. –  Rei Miyasaka Jan 15 '12 at 18:47
show 5 more comments

Unfortunately, there's not much about C++ that's basic, so I wouldn't get your hopes up. In addition, there's little you'll know about C# that's even remotely transferrable to C++, so I wouldn't expect any specific material to exist covering that angle. You'll need to learn from scratch.

share|improve this answer
3  
That would explain why there isn't much out there. After reading up on C++ it a bit it would appear that you are correct - it's a totally different world. Thanks for your help. –  Mark Williams Jan 2 '12 at 19:00
1  
+1, there's little you'll know about C# that's even remotely transferrable to C++ . I've learnt this the hard way. –  ApprenticeHacker Jan 3 '12 at 14:33
    
As a quick point, I would say there's plenty of C++ that's basic. Distinguishing pointers/references and C++ templates can make your head spin at first, but when it comes down to it there are still classes, cout and simple ways to achieve simple goals –  Kieren Johnstone Jan 4 '12 at 0:58
1  
@KierenJohnstone: You probably mean "slow and exception-unsafe ways to do things" like "array-to-pointer decay" and such things. –  DeadMG Jan 4 '12 at 18:44
7  
Nice completely unhelpful answer. –  dbracey Aug 17 '12 at 23:14
add comment

There's no easy fix for this, C++ isn't particularly difficult, but its not easy for beginners.

I would recommend reading up on the basics of C programming (to get a grounding of the low level stuff you'll find), then learn the STL, followed by articles from the likes of Scott Meyers. Here's one to get you started, as it shows how you should be writing modern C++, compared to C-with-classes.

share|improve this answer
2  
+1: I'd add getting Meyer's books (effective C++/STL series). I've got them all, and they're awesome. –  Steve Evers Jan 2 '12 at 21:54
11  
-1 for reading up on C programming. I think this does more harm than good and coveys the notion that C and C++ are languages that need to be studied together. –  sebastiangeiger Jan 3 '12 at 6:09
2  
@sebastiangeiger: you miss the point that when one has to work with existing real-world C++ applications you will most often have to deal with C-like constructs. Hence +1, at least to eliminate your downvote. –  Doc Brown Jan 3 '12 at 6:36
5  
@DocBrown Maybe, but I am on one of those C++ legacy projects right now and I can tell that my predecessors were largely just C programmers that saved their source files as *.cpp. I found that I am using a lot less delete and C arrays and other things that are discouraged in C++ than they did, which I attribute to NOT explicitly studying C. C is relatively simple, you can figure out pretty quickly what the code does. –  sebastiangeiger Jan 3 '12 at 7:08
2  
@gbjaanb: There's nothing, at all, about the STL that requires knowing malloc and free. A great C++ dev will eventually want to know about them. Starting with them is a whole different thing entirely. –  DeadMG Jan 4 '12 at 20:21
show 4 more comments
  • Look out for memory management. Get clear idea on how pointers and references work in C++ in contrast to C#. Also, RAII, manual memory management and smart pointers at last.
  • Templates and generics looks similar but are different beasts. Know the internals of how templates work.
  • There are some tiny but catchy syntactic differences. Learn them.
  • You have get used to STL instead of .net BCL. You may going to miss some good bits if have LINQ addiction.
  • There are some fundamental design difference between C++ and C#. So, you cannot just port an idea. Anything you build with C++, you have to design from scratch.
  • Goodluck
share|improve this answer
add comment

I'm not sure if you're still looking for materials, I found this article a few months ago. It isn't a book (only 53 pages). The introduction says it all:

This is a somewhat short guide to the important things to know if you are a C# programmer and find yourself needing or wanting to work in C++

http://geekswithblogs.net/mikebmcl/archive/2012/02/02/c-to-c-ndash-a-somewhat-short-guide.aspx

And there is also an update post too:

http://geekswithblogs.net/mikebmcl/archive/2012/02/14/c-to-cndasha-somewhat-short-guide-update-1.aspx

Happy learning :)

share|improve this answer
    
Be very careful when using this document though, it shows C++ in a very Microsoft centered way (it also says so). It does not differentiate between C++03 and C++11 features, and sometimes even shows non standard extensions as the preferred solution. You will end up with highly unportable code if you follow that guide. –  Fabio Fracassi Aug 18 '12 at 12:07
add comment

I think it is good that you dont find too many C# to C++ tutorials. They may probably muddle up your brain. Its a lot like trying to learn riding a motorcycle first and then switching to a bicycle (not an entirely appropriate anology but you get the idea). It is better for you to take a fresh book on C++. Since you are already familiar with the OOP concepts you can either skip the lengthy introductions to OOP concepts( they are meant for people migrating from C to C++). Or go diretly for a reference book meant for begginer-to-intermediate or intermediate levels.

share|improve this answer
add comment

I would recommend going to Rosetta Code and comparing the two. While you are there keep handy a link of C++ idioms and try to spot them. Then spend some time on the pointer/reference/memory/destructor realm. After that learn about the preprocessor -- it can really be your friend when it comes to hiding the uglies, I kind of miss it. Finally, the STL.

share|improve this answer
add comment

So, about one year ago I was in the same boat as you: self taught c# dev who felt the need to learn C++. Granted, I stopped and started multiple times. On my third attempt, I finally stuck through it.

What is imperative to understand is that, in terms of C++ and C# methodologies, you will likely spend a lot of time understanding what data manipulation methods through pointers and references really means, as well as when and how to use them given the circumstances.

The nice thing about C++ is that object stack allocation is quite simple for the most part, and you should allocate objects this way if your program will meet the requirements specified under this method. Otherwise, you rely on pointers for speed, efficiency, and quick clean up. I recommend reading this: http://www.parashift.com/c++-faq-lite/ as it will give you a LOT of technical understanding of how C++ works under the hood.

Let me give you an example:

In C++ there exists this constructor implementation known as an "initialization list", which differs from initializing class members in the body of a constructor. Why? Because unless the type being initialized is primitive, the compiler will actually create multiple, and unnecessary copies of the object being created when allocated in the ctor's body. This can potentially reduce performance, depending on the compiler as well as the scaled requirements of the app.

Know what a delegate is? Well, in C++ it is known as a function pointer. This language is quite difficult to master at first, but if you want to be awesome... it's definitely required.

C# is easy...very easy.

share|improve this answer
    
C# Delegates and c function pointers are quite different. A delegate is closer to a functor in C++ than to a function pointer. –  CodesInChaos Nov 24 '13 at 15:57
add comment

Believe me, Pick a good book on C++ (there are much more, just do a search) and start learning...

share|improve this answer
1  
BTW, learning some C before (pointers, arrays, struct) will be good –  Muhammad Hewedy Jan 3 '12 at 10:24
7  
Learning C before C++ will throw you back into the stone age. Nowadays, we very rarely use arrays and pointers in C++. The language has evolved away from them a long time ago. –  FredOverflow Jan 3 '12 at 14:25
7  
@Muhammad: Everyone else prefers speed, safety, ease of maintenance. –  DeadMG Jan 3 '12 at 18:41
3  
@DPD: Well, many professional C++ programmers, including Bjarne Stroustrup (the inventor of the language), disagree with you. His latest book teaches using objects and templates from the beginning, whereas arrays and pointers aren't talked about at all before Chapter 17 (page 500 and something). –  FredOverflow Jan 4 '12 at 11:15
2  
@DeadMG. Maybe, but you cant revert the C-> C++ evolution. If you hate the C part of C++ go for Java or C#. Personally, I found that concentrating on pointers and arrays in C before going to C++ helped me a great deal in my career. Sure , you can code a whole application without them but if you want the benefits of polymorphism you need to know pointers. How many of the important GoF patterns can be implemented without pointers? It wasnt the same for my friends who avoided the "hard" parts when studying C. Most of them found it difficult to understand polymorphism. –  DPD Jan 5 '12 at 5:27
show 7 more comments

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.