Skip to content
BoKSA

Introduction to C++

Introduction to C++

Introduction

You learned C: variables, pointers, structs, and functions. C++ started as "C with classes" and grew into a larger language — but on embedded targets it is still one program, one binary, talking to the same registers and the same silicon.

This article answers: what does C++ add, what stays the same, and why firmware teams (and Arduino, mbed, ROS2 nodes on Pi) use it instead of or alongside C.


C++ is a superset — mostly

Valid C code often compiles as C++ with few changes. Your int, struct, pointers, and malloc still exist.

1
2
// Still valid C++ (compiled as C++ though)
int sensor_value = adc_read();

C++ adds features on top:

Idea What problem it solves
Classes Bundle data + functions that belong together
Constructors / destructors Run setup and cleanup automatically
References Safer "alias" than raw pointers for parameters
Namespaces Avoid name clashes (HAL_GPIO vs gpio::write)
Templates Reuse code for uint8_t and uint16_t without copy-paste
Stronger typing enum class, bool, fewer implicit conversions

In plain terms

C gives you bricks and mortar. C++ gives you prefab walls (classes) and automatic doors that close when you leave the room (destructors). The foundation is still bricks — memory and CPU rules do not disappear.


Same machine, different compiler front-end

You still cross-compile for the MCU. The driver is often called g++ or arm-none-eabi-g++ instead of gcc, but the pipeline is the same: preprocess → compile → assemble → link (see GCC toolchain in depth).

1
arm-none-eabi-g++ main.cpp sensor.cpp -o firmware.elf

.cpp / .cxx tells the toolchain to use C++ rules. Mixing is normal: C drivers (hal_spi.c) linked with C++ application (main.cpp). C++ code calls C via extern "C" (covered in Namespaces and code organization).


Why teams choose C++ on embedded

Benefit Example
Encapsulation Sensor driver hides register details; app sees read()
RAII Mutex lock released when scope ends — fewer forgotten unlocks
Type safety enum class — no accidental if (mode = 3)
Reuse Template ring buffer for any element type
Ecosystem Libraries (Arduino, mbed) written in C++
Cost Example
Flash size Templates and inline functions can duplicate code
Complexity More language features to restrict on small targets
Tooling Longer compile times, need clear style guide

Many products use C for HAL, C++ for application logic — best of both when boundaries are clear.


Hello world — C vs C++

C (you know this):

1
2
3
4
5
6
#include <stdio.h>

int main(void) {
    printf("Hello\n");
    return 0;
}

C++ (iostreams — common on hosted, rare on bare MCU):

1
2
3
4
5
6
#include <iostream>

int main() {
    std::cout << "Hello\n";
    return 0;
}

On MCU firmware you still often use printf or UART hooks — std::cout needs support code you may not have. C++ on embedded is not about iostreams; it is about classes and RAII with the same printf you used in C.


C++ versions — C++11 matters

You will see C++98, C++11, C++14, C++17, C++20. For embedded courses and modern toolchains, assume C++11 or newer unless an old compiler forces otherwise.

Useful C++11+ features on embedded (details later):

  • nullptr instead of NULL
  • auto for ugly iterator types
  • Range-based for
  • enum class
  • constexpr for compile-time constants
  • Move semantics (advanced; often skipped on tiny MCUs)

Flag example: -std=c++17


What C++ does not magically fix

  • Stack overflow — still possible with deep calls or large locals
  • Race conditions — still your job; C++ threads exist but RTOS rules apply
  • Hardware timing — still nanoseconds and interrupt latency
  • Memory limitsnew can fail; heap may be tiny or disabled

C++ helps you express intent more clearly. It does not replace Pointers in C or Real-time systems.


Relevant topics


Starting points

  1. Compile one .c file as C++ (g++ file.c) — note warnings C++ adds.
  2. Find one .cpp file in a framework you use (Arduino sketch, mbed example).
  3. List three C pain points in a project (global state, init order, cleanup) — guess how C++ might help.
  4. Compare .elf size of minimal C vs C++ main on your MCU toolchain.

Focus points

  • Learn C memory first — C++ sits on top of it.
  • Subset the language on MCU — not every C++ feature belongs in firmware.
  • Mix C and C++ deliberately — extern "C" at boundaries.
  • Measure flash when introducing templates or exceptions.

Key points

  • C++ extends C with classes, RAII, namespaces, templates, and stronger types.
  • Toolchain and memory model are the same; compilation uses g++ rules for .cpp.
  • Embedded teams use C++ for structure and safety, not for desktop-only features.
  • C++11+ is the practical baseline; always check your compiler version on the target.