What can I expect when I switch from C & C # to C ++?

Here is a simple question.

I did a lot of work using both C and C # (2.0), but never anything in C ++. What can I expect when learning C ++? Will there be any big problems or obstacles that I should pay attention to? Does anyone have good course / website guidelines for learning C ++ for an experienced programmer?

+4
source share
14 answers

The main difference I can think of is that C ++ is a lot more multi-paradigm language than C and C #. In C #, OOP is still a paradigm. This is the OOP language first of all, and if you are not doing OOP, the C # community will tell you that you are doing it wrong. (although C # has added good support for several bits of functional programming, but over the past few years).

In C ++ OOP, well, it is supported, and you can use it when you like it, but the whole problem is related to general programming. C ++ templates allow you to use a wide range of smart, reusable, and universal libraries and achieve many goals, such as old-fashioned OOP, but without large inheritance hierarchies and virtually no communication between classes. The standard library contains many examples of this.

In C ++, many C constructs, although legitimate, mostly avoid:

  • Raw pointers (usually replaced by smart pointers such as boost::shared_ptr or std::auto_ptr , or with links
  • Memory allocation in user code (usually should be included in a smart pointer or in a custom RAII object)
  • Function pointers (usually replaced by functors, to increase security and performance)
  • goto (often used in C to switch to cleansing code. Again, made unnecessary with RAII)
  • preprocessor (almost never needed. Prefer templates instead)

Of course, there are exceptions to each of these points, but, as a rule, C ++ code, unlike C code, largely eliminates all of their use.

And more than in C #, classes are really workhorses doing a lot of hard work. In C #, a class is a little more than a few forests, a container in which all your methods are stored. Of course, he has a constructor, and he can implement Dispose (); but C ++ takes this a lot more, and you have:

  • Constructor (as in C #, which initializes the class from scratch)
  • Copy constructor (initializes the class as a copy of another object)
  • Assignment operator (since classes are what C # will consider value types. And therefore, an assignment is not just a change to a link, but copying over the entire contents of an object defined by the user)
  • Destructor

The destructor is probably the most important concept in C ++. This is vital for RAII, which is controlled by memory or other resources, because it is automatically called when an object is out of scope. This allows your classes to fulfill many guarantees that cannot be achieved in C or C #. For example, boost :: thread provides locked locks that are guaranteed to be released when you exit the scope, whether the function returns normally, an exception is thrown, or something else. Therefore, when using this library, the user does not need to worry about releasing locks or other resources. It just happens automatically as soon as you are done with them.

In a way, this gives you a lot more hooks to customize the behavior of your class. Unlike C #, you precisely control what happens when a simple assignment is executed. You control what happens when a class goes out of scope, when it is initialized from scratch, or as a copy of another object. This allows you to write the class correctly, which will be practically impossible to use incorrectly. (Nearly)

In addition, templates and metaprogramming templates are concepts you are likely to come across. They are very powerful tools, so make sure you are friends with them. :)

+14
source

Here are some of my points of view:

  • Pointers play a big role in C ++. So far in C # you only have reference types. And you may get a headache from a pointer error.
  • You also need to manage the allocation of memory manually. It is also possible that your program will experience a memory leak if it does not execute properly. In C #, every object collects garbage automatically.
  • Declarations and definitions classes are usually separated into a header file (.h) and a source file (.cpp or .cc) . This will lead to a bit of confusion at first.
  • You will not have as many base class library support in C ++ as in C #. For example, you will not have the HttpRequest built into C ++.
  • Therefore, you will have to use many external libraries and know how to handle .dll or .lib.
  • C ++ does not have an Interface , as in C #. You will use an abstract class (and multiple inheritance). Line
  • in C ++, it is a char array or a pointer to a char.
  • There are some algorithms and data structure available for use in the Standard Libary Template (STL.) But you need time to learn before you can start using it effectively :)

You can also learn C ++ / CLI to combine your .NET code and native C ++ together to get the best out of the world.

+8
source

I highly recommend: "Effective C ++ and more efficient C ++." And if you are at it, an "Effective STL" might be helpful :)

These books were written specifically for smart programmers who need to efficiently handle C ++. I confirm that effective C ++ was the one that helped me a lot when I started C ++!

Another good book for a C ++ novice (but I don’t think a novice computer) is Accelerated C ++. It focuses on good coding, stl and creating high-level code instead of focusing on the details. (they are described in more detail at the end of the book).

good luck :)

+4
source

Well, I've been using C ++ for over 10 years now, so I would answer by studying strict memory control, C ++, STL templates, then Boost. It will take you several months, then nitty gritty will tell you 5 years :)

As for books, I like Herb Sutter Exceptional C ++ and more exclusive C ++

+3
source

Hmmm ... Tough. If you're fine with .NET and pointers, there shouldn't be too much new.

I think working with C ++ header files will be an experience. There is also C ++ STL to meet you.

You may also have to deal with people who yell at you about things like multiple inheritance.

+2
source

I switched from Asm to C, C ++, ... and most recently, C #. This makes it easier to create objects and simply returns them from a method or property. As a rule, neither the implementation nor the user code should worry about freeing this memory. In addition, there is no reason to return a status code, such as HRESULT, in most cases, because if there is a problem, you throw an exception and let the user code handle it if he wants to.

Having recently returned to the original C ++ code for my last project, I really missed the garbage collection and throwing exceptions.

However, I like the features of C ++ templating. C # must one day expand this technique.

+2
source

I see that a few people point to memory management as a big problem, because you have to do it manually. Well, do not believe them, this is wrong. If you are not in an exotic / old-fashioned environment, there are tools in C ++ that help us manage memory (as well as any other resources) implicitly and deterministically. See Boost / std :: tr1 shared_ptr <> and RAII.

The big difference with the GC memory collected: you have to do the loops yourself.

As for multiple inheritance, once you understand what LSP means, this is not a problem either.

I need to agree with the pheze post : Accelerated C ++ should be read if you want to teach you how C ++ can (/ should?) Be used.

+1
source

Jalf has it absolutely right - from the background of C / C #, you practically already know all the basic C ++ that you need, and you also know OOP, so you are as good as you did.

There are two things that come to mind that will be new to you:

  • RAII: this allows you to automatically manage memory, you no longer need to forget to free everything that you malloc. Cool, yeah! It also helps with any other resource, you no longer need to forget to close this socket, file, connection to the database - RAII does this for you. Neither C nor C # have this.

  • STL / Boost: This is a "C ++ Class Library" full of useful utilities and bits. All standard containers are in the STL (for example, dictionaries and arrays and lists) along with algorithms that you can apply to them (for example, if you have a list of data items, you can provide a small function object (called functor), and then pass both algorithms of a general nature.This is really powerful, you can do a ton of work using it).

+1
source

The main difference is that C ++ is based on value, and C # is based on links.

In C #, when you pass an object to a method, you are actually just passing a reference, so the calling and called methods look at the same object.

In C ++, when you pass an object to a method, a copy of that object is created, and the entire copy is passed to the method. Changes made by the called method to the copy do not affect the original in the calling method. Figurative behavior can be modeled in C ++ using the reference syntax:

 void somemethod(someclass& obj); 
+1
source

The biggest "obstacle" is likely to be that you will be responsible for freeing the memory you have allocated.

0
source

As previously reported, you will probably be responsible for managing memory, but others, and then just study the C ++ libraries and how to include them, I think this should not be a difficult switch at all.

0
source

As for the differences in language implementation - and I think you need to be careful with things like memory management, heading declarations, etc. - I think that the most difficult thing in this is the additional overload of special characters in the syntax. After many years of writing C # code, what I come across the majority are those extra * , & and <> . My first reaction is that C ++ code is very similar to a big regular expression.

I am sure that as soon as you use it for a while, it will disappear, but I no longer do C ++ to be convenient, that I know exactly which character to use in each case - I pass a pointer or a link to this object ?

Another big mistake when reading other people's code is operator overloading. I know that you can do this in C #, but I rarely saw how it was done. Maybe everything has changed, but I often saw operator overloading in C ++, where +/- used some strange effects. Besides some fairly obvious victories (e.g. string concatenation), I think this is a really dubious function that I could manage with - both in C ++ and C #.

0
source
  • Memory management
  • More complex syntax
  • Memory management
  • Less tool support than C # for the entire OOP life cycle.
  • Memory management

And did I mention memory management?

I have always considered it interesting to use the market as an indicator of the perceived needs of part of the population. Given (for contrast) the VB market, it would seem that library components are the main focus. However, from the earliest days, the dominant offerings in the C ++ (and C) market were apparently memory management, leak detection, and tools to improve code quality (for example, lint-like inspectors).

0
source

Loops between classes [in C ++]. OOP in C ++ is really half baked if you ask me.

edit: Okay, so some details are fine, I suppose. Let's say you have: a Foo class and a Blah class. The Blah class refers to the Foo class, the Foo class refers to the Blah class. Suppose you go a naive route that you can implement like this:

 #include <blah.h> class Foo { Blah blah; ... } 

and blah:

 #include <foo.h> class Blah { Foo foo; ... } 

It probably won't work. Instead, you need to use direct links, i.e. Foo becomes:

 class Blah; class Foo { Blah blah; ... } 

If you do some kind of search link for "circular links in C ++", you will understand why I mentioned this in this question. Now stop voting for me, I ...

-3
source

All Articles