friend — access for a nominated function or class
A friend declaration lets a nominated function or class access private and protected members without making it a member of the granting class.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
A friend declaration lets a nominated function or class access private and protected members without making it a member of the granting class.
The goto statement unconditionally transfers control to a named label in the same function. It can exit multiple nested loops, but remains subject to the rules for initializing and destroying local objects.
The if statement selects a branch using a condition contextually converted to bool. Learn optional else branches, else if chains, initialization before the condition, and the distinction between ordinary if, if constexpr, and if consteval.
`import` makes a named module's exported interface, a partition of the current module, or a header unit available. It does not textually paste contents like `#include`; importing a named module does not make macros available.
inline allows function and variable definitions in headers while respecting the one-definition rule. It does not force call-site substitution or guarantee a faster program.
`module` specifies that a translation unit belongs to a named module or starts a module fragment. Distinguish `export module M;`, `module M;`, and `import M;`: they declare an interface, an implementation, and use of a module, respectively.
The mutable keyword allows a selected data member to change even in a const object. In a lambda it has a different role: it removes the call operator's default const qualification, allowing modification of modifiable copies of captured values.
The namespace keyword defines a namespace or a namespace alias. Namespaces group declarations and help distinguish identically named entities; qualified access takes the form N::name.
noexcept either declares that an exception cannot escape a function or checks at compile time whether an expression is potentially throwing. These two uses combine in the conditional specification noexcept(noexcept(expression)).
`not` is an alternative spelling of the `!` operator in C++. The built-in operator reverses its operand's truth value and returns `bool`. It is neither bitwise negation nor a separate library function.
`not_eq` is a language-level alternative spelling of the C++ operator `!=`. It compares two operands using the same rules, precedence, and associativity; it requires neither a header nor a macro.
nullptr is a null pointer constant with the distinct type std::nullptr_t. Use it to initialize and compare pointers instead of the numeric literal 0, especially when calling overloaded functions.
The operator keyword forms names for overloaded operator functions, conversion functions, and literal operators. It lets you define how operations work on your own types, without changing operator precedence or grouping.
`or` is a language-level alternative spelling of the C++ operator `||`. For the built-in operator, the result is `bool`, and the right operand is evaluated only when the left operand is `false`. Do not confuse it with bitwise OR: `|` or `bitor`.
`or_eq` is an alternative spelling of the C++ `|=` operator. It combines the operands' bits using OR and stores the result in the left operand. It is a reserved language token, not a macro, and requires no header.
override requires a member function to override a virtual function from a base class. It catches declaration mistakes during compilation instead of allowing an inherited function to be hidden accidentally.
The private keyword restricts access to class members to the class's members and friends. In a base clause, it specifies private inheritance. This is a program correctness check, not a memory-security boundary.
The protected keyword controls member access or inheritance access. It makes members available to derived classes without exposing them to ordinary outside code, but adds restrictions on the object expression used to access non-static members.
The public keyword specifies public access to class members or public inheritance. A public: section exposes a class interface, while : public Base preserves the public and protected access levels of base-class members.
In C++23, register remains a reserved keyword but is not a permitted storage-class specifier. Its former use in declarations was removed in C++17; migration usually only requires removing register.
The requires keyword introduces either a clause constraining a declaration or an expression checking requirements on types and operations. Distinguishing these uses helps you write templates whose availability depends on the capabilities of the supplied type.
The return statement ends the current function call and transfers control to its caller. It can also initialize a returned value or reference. It is not equivalent to break: inside a loop, it exits the entire function.
The signed keyword specifies a signed integer type. Used alone, signed means int; with short, int, long, and long long it is redundant. However, signed char is a distinct type from both char and unsigned char.
The meaning of static depends on the declaration's location: a local variable retains state between calls, a namespace-scope name gets internal linkage, and a class member does not require a separate copy in every object. Storage duration, scope, and linkage are different concepts.