Selected keywords
suggest changeThis page summarizes several C++ keywords. Their detailed behavior is covered in the linked topic pages.
void
As a function return type, void means that the function does not return a value. In a parameter list, void is equivalent to an empty parameter list in C++, although f() is the idiomatic spelling.
void* is a pointer to an object of unknown type. A pointer to any non-const object can be converted to void* and back to its original pointer type. It must be converted to an appropriate object-pointer type before it can be dereferenced. Unlike C, C++ does not implicitly convert a void* to another object-pointer type.
int value = 42;
void* untyped = &value;
int* typed = static_cast<int*>(untyped);
An object pointer and a function pointer are distinct categories; portable C++ does not guarantee conversions between them.
volatile
volatile tells the implementation that accesses to an object are observable side effects and therefore must not be optimized away like ordinary memory accesses. It is useful for some memory-mapped I/O and low-level implementation-specific code.
volatile unsigned int* status_register = /* implementation-defined address */ nullptr;
volatile does not make compound operations atomic and does not provide inter-thread synchronization. Use the facilities in <atomic> for shared state accessed concurrently by multiple threads.
virtual
The virtual keyword declares a virtual member function or a virtual base class. Calls to virtual functions through base-class references or pointers can dispatch to an override selected from the object's dynamic type.
struct Base {
virtual ~Base() = default;
virtual void update() = 0;
};
struct Derived : Base {
void update() override {}
};
this
Within a non-static member function, this points to the object on which the function was called. Static member functions have no this pointer. Member access normally uses this implicitly, but it can be written explicitly:
class Date {
public:
Date& set_month(int month) {
this->month_ = month;
return *this;
}
private:
int month_ = 1;
};
The expression *this refers to the current object and is commonly returned by reference from mutating operations that support call chaining.
try, throw, and catch
A try block encloses code that may throw an exception. A matching catch clause handles it. Exceptions are commonly thrown by value and caught by const reference:
#include <stdexcept>
void connect(bool available) {
if (!available) {
throw std::runtime_error("Unable to connect");
}
}
int main() {
try {
connect(false);
} catch (const std::exception& error) {
// Handle or report error.what().
}
}
A catch (...) clause matches any exception. A bare throw; inside a handler rethrows the currently handled exception.
friend
A class can grant a non-member function or another class access to its private and protected members. Friendship is granted by the class being accessed; it is neither inherited nor reciprocal.
class Counter {
friend void increment(Counter& counter);
int value_ = 0;
};
void increment(Counter& counter) {
++counter.value_;
}
A friend function is not a member and is not called with . or -> unless it is independently a member of another class. A friend declaration can appear in any access section because its meaning is unaffected by public, protected, or private.