Lesson 207 +5 XP

C++ Keywords Reference

C++ Keywords Reference

Keywords are the words C++ reserves for itself. You cannot use them as variable or function names - the language already owns them. This is your lookup table for the most common ones.

Type keywords

KeywordPurpose
intdeclares a whole-number (integer) variable
floatdeclares a decimal number (single precision)
doubledeclares a decimal number (double precision)
shortinteger, usually smaller than int
longinteger, usually larger than int
chara single character, like 'A'
boola true/false value
autolets the compiler figure out the type for you
void"no type" - used for functions that return nothing
constmarks a value that must not change
constexpra value computed at compile time (C++11+)
KeywordPurpose
structgroups data fields into one type
uniona type where all fields share one memory spot
enuma set of named constants
unsignedan integer type that holds only 0 and above
signedan integer type that can be negative (default)
boola true/false value (same as bool)
stringa sequence of characters (from the std library)

Control-flow keywords

KeywordPurpose
ifrun code only when a condition is true
elserun the alternative branch when if was false
else ifchain a new condition after a false if
switchpick one of many branches based on a value
caseone branch inside a switch
defaultthe "else" branch of a switch
forloop with a start, condition, and step
whileloop while a condition is true
doloop once, then keep looping while a condition is true
breakleave the nearest loop or switch early
continueskip the rest of this iteration of a loop
returnsend a value back from a function and stop it
gotojump to a labeled line (avoid it)

Class and object keywords

KeywordPurpose
classdefine a type with data and functions
structlike a class, but members are public by default
publicmembers accessible from outside the class
privatemembers accessible only inside the class
protectedmembers accessible inside the class and its subclasses
thisa pointer to the current object inside a class
newallocate memory on the heap and build an object
deletefree memory created with new
virtualallow a member function to be overridden by subclasses
friendgrant an outside function/class access to private members
templatewrite code that works with any type
typenameannounces a generic type parameter in a template
operatordefine how an operator behaves for your class
inlinehint that a function should be inlined at the call site
explicitstop the compiler from using a constructor implicitly
overridestate that a function overrides a base-class one (C++11)
finalstop a class, function, or virtual from being further overridden (C++11)

Namespace and linker keywords

KeywordPurpose
namespacegroup names under one label
usingusing namespace std; pulls names into scope
externdeclare a variable/function defined in another file
inlinesuggest the function be inlined at each call
autolet the compiler deduce the type of a variable
throwraise an exception
trystart a block that may catch exceptions
catchhandle an exception thrown in a try block
typenamename a dependent type inside a template
registerask the compiler to keep a variable in a CPU register (deprecated by C++17)
volatiletell the compiler a variable may change from outside the program
mutablea member that may change even inside a const object

Notes

  • Don't name your variables, functions, or classes with any of these - the compiler will reject them (or behave badly).
  • static shows up in three places with three meanings: file-local variable, class member owned by the class, and storage inside a function. The trick is reading the surrounding context.
  • const and constexpr both mean "won't change"; constexpr upgrades that to "computed before the program even runs".
  • The throw / try / catch words power exception handling. Seeing throw means "something went wrong here - pass control to a catcher".

TL;DR

  • Keywords are reserved words the language owns as its own names.
  • Biggest groups: type keywords, control-flow keywords, class keywords.
  • void means "no value", const means "read-only value".
  • Never use a keyword for variable or function names.