C++ for C# Developers: Table of Contents

November 5, 2012 2 comments

After more than two months of preparation, now I’m ready with the planned table of contents of the Book.

Contents at a Glance

The book is divided into five parts that let you progressively learn C++ and use in the practice to create – among the others – modern Windows 8 apps.

Part I: Getting Started with C++ and Visual Studio

In this part you will learn why the C++ programming language is important today, and you get acquainted with the basic characteristics of the C++ language, and using it with Visual Studio 2012, too.

Chapter 1: The Renaissance of C++
Chapter 2: Tasting C++
Chapter 3: Understanding the C++ Compilation Process
Chapter 4: Using Visual Studio with C++

Part II: C++ Programming Language Fundamentals

In this part you will learn about the fundamental ideas and elements of C++ – built on your C# knowledge. The constructs treated here are familiar for all C# developers, however, in the context of C++ they have different semantics. Understanding these conceptual elements is a key to possess the power of C++.

Chapter 5: Types, Values, and Variables
Chapter 6: Functions, Classes, and Namespaces
Chapter 7: Variables and Storage
Chapter 8: Pointers, Memory and Lifetime Management
Chapter 9: Statements and Expressions
Chapter 10: Exceptions

Part III: Understanding Advanced C++ Features

C++ has a number of concepts that either do not exist in C# or have very different semantics there. In this part you will learn about them.

Chapter 11: Operator Overloading and Conversions
Chapter 12: Copy and Move
Chapter 13: Advanced Class Concepts
Chapter 14: Generic Programming with Templates
Chapter 15: Run Time Type Information

Part IV: Using the C++ Library

Just as the Base Class Library in .NET and C#, the run time library of C++ is a fundamental tool to create applications. This part treats the essential C++ run time libraries and their most important usage patterns.

Chapter 16: The C++ IO Library
Chapter 17: Standard Template Library Overview
Chapter 18: Containers
Chapter 19: Algorithms
Chapter 20: Regular Expressions

Part V: Programming Windows 8 with C++

Windows 8 provides a new application development model. In this part you will learn the tools and techniques that allow you using C++ to create modern Windows 8 apps.

Chapter 21: Overview of the New Windows 8 Programming Model
Chapter 22: The C++ Component Extensions
Chapter 23: Windows 8 Programming Basics
Chapter 24: Creating and Using Windows Runtime Components
Chapter 25: C++ Accelerated Massive Parallelism


C++ is a huge topics. This book does not want to be a heavyweight one, instead, it wants to remain concise and focused. To reach its goal, this book intends to use the following approach:

  • Samples, samples, samples
  • Instead of the “learn-from-scratch” approach, it builds on your existing C# knowledge
  • This book treats the modern C++ language characterized by great new features of the C++11 (C++0x) standard.
    Portability is a very important aspect of C++. This book takes portability into account as key factor in learning C++. However, because this book is for C# developers, and majority of them uses Visual Studio, samples and exercises will be built up with Visual C++ in mind.
Categories: Book Tags:

How Program Compilation Works

October 5, 2012 2 comments

With Visual Studio it is quite easy to build your C# project and immediately run it. You just press the F5 key and the IDE immediately starts building your project, and when this phase completed successfully, it launches the application in Debug mode. If you want to run it without debugging, you can use the Ctrl+F5 key instead.

The same approach works for C++ programs. However, there are significant differences between the compilation process of C# and C++, and understanding it is an important step to get closer to C++. So, before going into details of using Visual Studio with C++, let’s examine these processes.

Compiling and Running C# Applications

In the Windows platform, C# applications use the .NET framework. When you build a C# application, the source code is compiled into an intermediate language (IL), often mentioned as MSIL (Microsoft Intermediate Language). This compilation consists of a few steps:

1. The compiler parses all source files and creates syntax three within the memory, which represents the structure of the program.

2. There are identifiers and names within the source code must be resolved, for example, a name such like System.String should be bound to the appropriate predefined type in a system assembly, or the _myPage identifier to a member field of the MyApp class. During this binding phase the compiler looks up identifiers (according to the C# Language Specification) in the source files of the project, and in referenced assemblies.

3. When bindings are done, the compiler emits the IL code that represents the result of the compilation (either as an .EXE or a .DLL file).

When you run the application, the IL code must be translated to CPU-specific code. This is done by the Just-In-Time (JIT) compiler. Without going into details, this compiler translates only the IL code about to be run, and not the whole executable file. Once a part of the IL code is compiled to CPU-specific code, it remains in the memory and next time when it should run, no compilation occurs again.

The .NET Framework includes the NGEN.EXE (Native Image Generator) that is a tool that improves the performance of managed applications by creates native images, which are files containing compiled CPU-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.

Compiling and Running C++ Applications

The compilation of a C++ projects results an executable (or as you learn later other kinds of artifacts) that contains CPU-specific code and so can directly run on the appropriate operating systems with the proper CPU. The compilation process that results this executable is totally different from the one of C#, as shown in Figure 1.


Figure 1: The compilation process of C++

The compilation of a C++ project contains two main steps:

1. The compiler parses each C++ source file, checks their syntax and semantics, and emits CPU-specific instructions to an object file—with the .obj extension—that may refer to external entities (types, objects, methods, etc.). During this step the compiler first uses a preprocessor that understands the directives starting with the “#” character, and changes the source file accordingly. For example, when the preprocessor meets the #include directive, it inserts the text in the specified file into the source code.

2. The linker takes all object files and resolves external references, and links them into an executable.

This approach is very different from the compilation of C#:

  • C# does not have any preprocessor.
  • The C# compiler uses the binding phase to resolve references and does not need a linker.

The responsibility of the linker can be easily explained with the “Hello, world” application you examined in Chapter 2:

#include “stdafx.h”
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
std::cout << “Hello, World!” << std::endl;

When the _tmain method is compiled in the object file it will contain references to the std::cout and std::endl objects as well as to the two “<<” operators (they are different ones, as you will learn later). A method call is translated to one of the CPU-specific CALL instructions that use the absolute or relative physical address of the machine code representing the entry point of the method. While in an executable file it is a real physical address, in the object file this is a reference to an entry in the object file’s symbol table. When the linker assembles the object files into an executable file, it uses the symbol tables of object files to resolve these references and change symbols with real addresses.

Not only method calls, but also external variable (and other data) references are resolved in a similar manner.

The real operation of the linker is more complex, but this scratchy explanation tells you enough to understand what behind the scenes goes.

The compilation of HelloWorld.cpp can be represented with the following ASM file (assembly output generated by the compiler with the /FAs command line option):

; Function compile flags: /Odtp /RTCsu /ZI
; File c:\users\istván\skydrive\cpp4csharp\samples\c++\helloworld\helloworld\helloworld.cpp
; COMDAT _wmain
_argc$ = 8 ; size = 4
_argv$ = 12 ; size = 4
_wmain PROC ; COMDAT
; 5 : {
push ebp
mov ebp, esp
sub esp, 192 ; 000000c0H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-192]
mov ecx, 48 ; 00000030H
mov eax, -858993460 ; ccccccccH
rep stosd
; 6 : std::cout << “Hello, World!” << std::endl;
mov esi, esp
mov eax, DWORD PTR __imp_?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z

push eax
push OFFSET ??_C@_0O@KLMCIIGF@Hello?0?5World?$CB?$AA@
mov ecx, DWORD PTR __imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A
push ecx
call ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z
; std::operator<<<std::char_traits<char> >
add esp, 8
mov ecx, eax
call DWORD PTR __imp_??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z
cmp esi, esp
call __RTC_CheckEsp
; 7 : }
xor eax, eax
pop edi
pop esi
pop ebx
add esp, 192 ; 000000c0H
cmp ebp, esp
call __RTC_CheckEsp
mov esp, ebp
pop ebp
ret 0
_wmain ENDP

Do not feel intimidated by this horrific code, as you need to understand only a few things in it. The numbered lines with shaded backgrounds are comments representing the source code lines. The terrifying mambo-jumbo names highlighted in this code are the symbolic names to be replaced by the linker. In order of appearance, these are the following symbols:

  • After mov eax, DWORD PTR you see the symbol of the std::endl object.
  • The next one in the push OFFSET instruction represents the “Hello, World” string literal.
  • The third symbol (after mov ecx, DWORD PTR) stands for the std::cout object.
  • The fourth and fifth symbols represent the two “<<” operators.

If you have ever worked with CPU-specific code, you can recognize that this code first pushes the std::endl, “Hello, world” and std::cout addresses to the stack, and then calls the appropriate methods representing the two << operations.

The C++ Preprocessor

In contrast to C#, the C++ compiler uses a preprocessor before starting to analyze the syntax and semantics of a source code file. The preprocessor is a simple text processing engine that transforms the source code according to preprocessor directives. The following example helps you to understand what that transformation means:

#include “stdafx.h”
#include <iostream>
#define LOWER_BOUND 0
#define UPPER_BOUND 9
#define MAX(a,b) ((a) > (b) ? (a) : (b))

int _tmain(int argc, _TCHAR* argv[])
for (int i = LOWER_BOUND; i <= UPPER_BOUND; i++)
std::cout << i << “: Max(” << i << “, ” << REFERENCE_NUMBER
<< “) is: ” << MAX(i, REFERENCE_NUMBER) << std::endl;
return 0;

The preprocessor directives can be easily recognized because they start with a # (number sign). In this sample you can observe two directives, #include and #define, respectively. As you already learned, #include instructs the preprocessor to read and parse the content of a header file. The #define directive defines a macro that contains an identifier and an expression separated by a whitespace. For example, in the first #define directive the identifier is LOWER_BOUND, and the expression is 0. This directive causes the compiler to substitute the expression for each occurrence of the identifier in the source file. The identifier is replaced only when it forms a token, but it is not replaced if it appears in a comment, in a string, or as part of a longer identifier.

The identifier of #define can be extended with function-line parameters. For example, MAX macro has two parameters, a and b. Each formal parameter in the expression is replaced by the corresponding actual argument. Any macros in the actual argument are expanded before the directive replaces the formal parameter.

You can instruct the C++ compiler of Visual Studio to output the preprocessed files (with the /P command line switch). Using this technique, you can see the result of these directives. After the preprocessor does it job, the definition of the _tmain function looks like this:

int wmain(int argc, _TCHAR* argv[])
for (int i = 0; i <= 9; i++)
std::cout << i << “: Max(” << i << “, ” << 4
<< “) is: ” << ((i) > (4) ? (i) : (4)) << std::endl;
return 0;

You can discover the LOWER_BOUND, UPPER_BOUND and REFERENCE_NUMBER macros have been substituted with 0, 9 and 4, respectively.

The MAX(i, LOWER_BOUND) macro has been replaced with ((i) > (4) ? (i) : (4)), because parameter a of the macro was i, parameter b was another macro—LOWER_BOUND—, and this latter stands for 4.

The expression of MAX(a,b) contains many parentheses. They are very important, because without them the parameter substitution may result in erroneous syntax or semantics. For example, if you omitted the outermost parentheses, and defined MAX(a,b) as (a) > (b) ? (a) : (b), your code would not compile. In this case the statement outputting to the console would look like this:

std::cout << i << “: Max(” << i << “, ” << 4
<< “) is: ” << (i) > (4) ? (i) : (4) << std::endl;

Because the precedence of the << operator is higher than the precedence of the ?…: ternary operator, the compiler would not understand the (4) << std::endl expression.

There are about a dozen preprocessor directives, you will learn about them later.

Categories: Uncategorized

Sample Chapter of the Book is Available

September 21, 2012 2 comments

Chapter 3I could write tons of articles about how I imagine the style of the book, but nothing would tell as much as a sample chapter. I have just released an early bit of Chapter 2 – Tasting C++ for your review.

In this sample I show you the general approach of the book I plan to follow:

  • Lots of sample code (both C# and C++)
  • Comparing the two languages, and pointing out the differences
  • Explaining C++ concepts based on what you know about C#
  • Giving you a performance comparison wherever it has significance.

Sample – Chapter 2 (PDF)

Any comments and suggestions are warmly welcomed.

Categories: Uncategorized

Welcome to My Blog!

September 21, 2012 1 comment

C4LogoSmallThis blog is my workspace for the book with the working title “C++ for C# Developers – with Windows 8 Programming”. I have created the blog to announce you the idea of the book, and let you know my progress. Of course, the aim of the blog is exactly the same as the goal of the book: I intend to help you get acquainted with the C++ programming language using your existing C# knowledge.

To let you understand why I got the idea of this book may be obvious for you, but let’s tell you my story…

Kids, if I’d intended to write this book ten years ago, its title probably had been “Programming .NET Framework in C#—for C++ Developers”. However, the world changed a lot in the last few years.

I had started learning C++ during my university years at the end of the 1980’s, and I used this great programming language till 1995. For a few years, I worked on a hospital information system project using Windows 3, which involved digital image management. That time when this project had been started, it was natural to program such systems in C++.

I always worked very close to Microsoft technologies, and the breeze of evolution reached my company, too. In 1994, I became a Visual Basic programmer, because that time VB meant the promise of easy and fast three-tier architectures. If you’d ever worked with Visual Basic—I mean the old one before .NET—, you may understand why I cheered up in 1996, when Borland released Delphi 1.0, and finally I could use a real object-oriented language after a fake one, again.

The world turned around for me when I heard about a new programming language, called C#. This moment in the summer of 2000 shepherded me to the herd of .NET developers and I became one of the evangelists of this modern framework and the C# language. I was happy with C# and after using it for a few years I slowly forgot about my roots. Sometimes it even happened that I scorned C++ developers because of their low productivity.

Most of us know that the only constant thing in this world is the change. The BUILD 2011 conference held in Anaheim, CA, where Microsoft introduced and released Windows 8 Developer Preview, for me was not only about the operating system, but also about C++. After attending at a few sessions upon this old-new programming language, all of a sudden I found my interest of C++ growing. At that time we—three of my Hungarian friends and me—had already signed a contract with Wiley to write a beginner’s book about Windows 8 application development, and it happened that I was the one undertaking the chapter about C++.

Those experiences led me to writing this book. After so much slight and being ungrateful with this old friend, I had to start learning C++ again. Instead of choosing the from-scratch-path, I decided to approach C++ from the direction of C#, as in the last ten years I had mostly C# in my head and my heart.

Kids, this is how I commenced writing this book.

Categories: Book Tags:

Get every new post delivered to your Inbox.