Comments are human-readable annotations embedded within source code. They are completely ignored by the compiler and are vital for explaining the intent, algorithms, and assumptions behind complex C++ implementations.
// 1. Single-Line Comment: Extends from // to the end of the current line
/* 2. Multi-Line Comment:
Spans across multiple lines
Ideal for longer algorithmic explanations */
Enterprise C++ codebases require structured documentation comments formatted using Doxygen tags:
/**
* @file math_utils.h
* @brief High-performance mathematical utility functions.
* @author Veewom Engineering Team
*/
/**
* @brief Computes the factorial of a positive integer recursively.
* @param n A non-negative integer.
* @return The calculated factorial as an unsigned 64-bit integer.
* @throws std::invalid_argument If n exceeds 20 (causing 64-bit overflow).
*/
unsigned long long calculateFactorial(int n) {
if (n <= 1) return 1;
return n * calculateFactorial(n - 1);
}
Do not comment on WHAT the code does (clear code should explain what it is doing). Comment on WHY the code was written in a particular way, highlighting non-obvious business logic, concurrency constraints, or edge-case handling.
Boost your C++ proficiency by studying these closely connected core modules:
How the preprocessor strips comments before compilation.
Documenting function parameters and return types.
Read well-commented student programs.