C++ For C# Developers: Part 25 – Intro to Templates

C# generics (List<T>) look a lot like C++ templates (list<T>), but they’re different in many key ways. It’s a big subject, so today we’ll start by looking at some of the most common uses of templates: applying them to classes, functions, members, lambdas, and variables.

Read the rest of this article »

C++ For C# Developers: Part 24 – Preprocessor

C# and C++ have similar lists of preprocessor directives like #if, but their features and usage are very different. This is especially the case in C++ with support for “macros” that can replace code. Today we’ll look into everything we can use the preprocessor for in C++ and compare with C#’s preprocessor.

Read the rest of this article »

C++ For C# Developers: Part 23 – Compile-Time Programming

The vast majority of the code we write executes at runtime. Today’s article is about the other kind of code, which runs during compilation. C# has very limited support for this. In C++, especially its newer versions, most of the language features are usable at compile-time. Read on to learn how to take advantage of this!

Read the rest of this article »

C++ For C# Developers: Part 22 – Lambdas

Both C++ and C# have lambdas, but they have quite a few differences. Today we’ll go into how C++ lambdas work, including all their features and how they compare and contrast with C# lambdas. Read on to learn all the details!

Read the rest of this article »

C++ For C# Developers: Part 21 – Casting and RTTI

Now that we’ve seen how types are implicitly converted in C++, we can see how they’re explicitly converted by casting. C++ offers a lot more kinds of casts than C# to control the conversion process. One of them—dynamic_cast—introduces the concept of Run-Time Type Information or RTTI, so we’ll go into that today as well.

Read the rest of this article »

C++ For C# Developers: Part 20 – Implicit Type Conversion

We’ve actually seen quite a bit of implicit type conversion so far in the series. We’ve converted integers to floats (float f = 123), arrays to pointers (int* p = a), base type pointers to derived type pointers (D* p = &b), and many more. Today we’ll gather all those casual conversions up into one article that goes over all the rules, including user-defined type conversions.

Read the rest of this article »

C++ For C# Developers: Part 19 – Dynamic Allocation

So far, all of the memory our C++ code has allocated has either been global or on the stack. For the many times when the amount of memory isn’t known at compile time, we’ll need dynamic allocation. Today we’ll go over both the basics of new and delete, but also dive into some advanced C++ features such as overloading new and “placement” new.

Read the rest of this article »

C++ For C# Developers: Part 18 – Exceptions

Like C#, C++ also uses exceptions as one of its main error-handling mechanisms. Today we’ll learn all about them: throwing, catching, their impact on destructors, what happens when they go uncaught, and so much more.

Read the rest of this article »

C++ For C# Developers: Part 17 – Namespaces

With structs wrapped up, we can move on to other features of C++. Today we’ll take a look at namespaces. We’ll cover the basics that C# provides, but go so much further and cover a lot of advanced functionality. Read on to learn all about them!

Read the rest of this article »

C++ For C# Developers: Part 16 – Struct and Class Wrapup

Today we’ll wrap up structs and classes by discussing a bunch of miscellaneous features: local classes, unions, overloaded assignment operators, and user-defined literals. C# doesn’t have any of these features, but it can emulate some of them. Read on to learn a bunch of new tricks!

Read the rest of this article »