tags:

views:

365

answers:

7

Ok. Here is a summary of my experience/knowledge/etc listed:

  • I have only worked with 2 languages: C# and F#
  • I am only familiar with programming in a managed environment
  • I have a solid understanding of C# syntax
  • I have a solid understanding of C#'s type system
  • I have never looked at a line of C++ code
  • I do not know C++'s type system

I am concerned that the C++ community/books/resources/etc will use similar words used in the C# community/books/resources/etc but mean slightly different (or completely different) things. It is possible this concern is not valid, but to stick with one question I will just ask; How should a .NET developer learn C++ ?

Note: I am learning C++ for interop purposes.

+7  A: 

You should find an application to write in it, that's the best way to learn it.

Lance Roberts
+1  A: 

i'd first learn C, if just to 'unlearn' all the C# extras.

then C++ would be easy, as long as you remember not to 'map' concepts

Javier
I'd go to C++ directly. Why would you want to go back to a procedural language when the OO concepts are very similar in C++ to begin with?
Rado
Quite. Too much bad C++ code out there is a result of peopel writing C in any language.
Steve Gilham
Going from C# to C to C++ only makes you do stupid C'ish stuff which isn't relevant in C++.
lhahne
for someone just wanting to use C++ for interop purposes, I actually think you could stick with C and completely ignore C++. Learning C++ is a big enough undertaking that it probably won't be worth it if you're only interested in being able to interop with native API's.
jalf
the worst OO designs i've seen is from people that doesn't know good non-OO designs. besides, i think the most important point is to unlearn C# style of OO and find the common ancestry. and finally, i think C is important to know _before_ going to higher level languages
Javier
+3  A: 

Just pick up a C++ book or use an equivalent online resource. At the moment, I can't think of a single aspect of C# that means something totally different in C++. Here are some of the main differences that you may want to take a closer look at (off the top of my head):

  • C++ allows multiple inheritance
  • preprocessor directives in C++ are very powerful (and confusing at the same time)
  • pointers

[edit] Forgot to mention another very important difference: There is no default garbage collection in C++. You either have to manage your dynamic memory manually, or enable garbage collection. Here is one implementation of garbage collection in c++.

Rado
Keep this in mind, learn the syntax, and understand that there are no using(), linq, string, etc. magic in C++ :) You will find yourself hardcoding a lot of boilerplate-code the first few times you use it, which is good for learning the language. After a while you'll grow tired of it and begin to look into Boost :)
cwap
This is helpful. Thank you.
beef
I can think of one thing that has a completely different meaning. Destructors, or the ~destructor() syntax specifically. But it's such a key feature of C++ that the difference very quickly becomes clear.
jalf
+1  A: 

I would recommend starting with Koenig & Moo Accelerated C++ -- probably the only C++ introduction that doesn't fall into the C-with-knobs-on trap.

Steve Gilham
+3  A: 

If you're doing this for interop, there are two things to bear in mind.

  1. You don't need to learn the entirety of C++. Keep it simple. You mostly likely need to know enough to call APIs designed for use from C. So you'll mostly be using the C-like subset of C++, but with the added convenience of some use of standard C++ container types for dynamic allocation (e.g. to allocate a vector of structures). In C++, being able to use a templated container is a far cry from being able to design one yourself, and fortunately you'll only need to use them.

  2. The most powerful, flexible way to expose C++-developed code to .NET clients is to use C++/CLI, so you need to get the basics of that; in particular, understanding how to do the equivalent of C# in C++/CLI. This can get confusing.

Given a .NET type C that supports IDisposable, this C++:

C c;
c.Foo();

is equivalent to this C#:

using (C c = new C()) 
    c.Foo();

Whereas this C#:

C c = new C();
c.Foo();
c.Dispose();

would be equivalent to this C++/CLI:

C ^c = gcnew C;
c->Foo();
delete c;

So, plenty to read up about there. It's all on MSDN.

Daniel Earwicker
This is extremely helpful and very close to the route I had in mind. Thank you.
beef
yup, +1. If you're interested in C++ for interop purposes, there's just no point in trying to completely learn and understand one of the most complex languages in existence. Learn the basics, and stick with the C-like subset (and C++/CLI) that's required to work with native API's.
jalf
A: 

We all learn best in our own ways. In your question, you implied that you have already learned two languages and you specifically asked about a ".NET developer" which also implies someone who has learned at least one language. So how should a .NET developer learn C++? Answer: The same way he/she learned the other language(s) they know. If you learn best from books, get a book. If you learn better from working your way through writing a program, then do that. Use whatever methods you have already proven to work for yourself.

In terms of content (rather than method) I do suggest that you learn about Visual C++/CLR rather than ANSI C++ alone since your goal is interoperability in the .NET environment. The C++ language is supported pretty well in the current .NET environment. There are absolutely plenty of differences, but I think you will be surprised at how much is the same as, or very similar to, C#. I think you will also find that you do not have to master the entire C++ language in order to write interop code. In fact, if your COM objects are small and simple, you may discover you can use the type library importer for interop if for some reason you cannot, or do not want, to use C++. There are some serious downsides and compromises to this approach, so be sure to check MSDN and the MANY interop Q&A on stackoverflow for details.

TMarshall
"Answer: The same way he/she learned the other language(s) they know..."I understand why you would say this. But it took a great deal of work and energy going the route I did with the last languages I learned. I think it is possible there is a more efficient way in learning. That is why I am asking the question.Your response is helpful though. If I can achieve Reverse P/Invoke using only C# I will definitely want to know this! But that is another question.
beef
I am saying use the same method of learning that you already have proven to work for you. If you feel you have been going about this the wrong way, then by all means experiment. All methods will require some work. But if you understand the basic programming fundamentals, then you do not start from scratch each time you learn a new language. When you learned your first language, you also had to learn many other aspects of development that you do not have to re-learn. So the amount of work required to learn your 3rd language should be less than that required to become proficient in your first.
TMarshall
A: 

from the response above...

I think you will also find that you do not have to master C++ in order to write interop code for managed and unmanaged clients.

How do you propose he writes an unmanaged client app in C#? (granted you can expose a C# assembly as com visible, but thats not the same thing)

I think it is pretty simplistic to imply that all your COM interop needs are met by doing interop from C#.

Anonymous Type
I thought my answer was clear, but apparently, it is not. I was not trying to imply that all COM interop should be, or even COULD be done from C#. I'm sorry if I gave that impression. I'll edit my answer to make that point. I also did not propose anyone write an unmanaged client app in C#. That's just not possible, or desirable. I suggested it is not necessary to MASTER the entire C++ language in order to write interop code. I stand by that statement.
TMarshall
yep fair rebuttal. i agree 100%.
Anonymous Type