Complete Introduction to C++ Keywords (Reserved Words)
Category Programming Technology
1. asm
asm (assembly string): Allows embedding assembly code within a C++ program.
2. auto
auto (automatic) is a storage class specifier indicating that a variable has local scope, and block-scoped variable declarations (such as those inside for loops) default to auto storage class.
3. bool
bool (boolean) type, a fundamental data structure in C++ with values either true or false. The bool type can be mixed with int, where 0 represents false and non-zero represents true. It is commonly used for condition checks and function return values.
4. break
break (interrupt, exit), used in switch statements or loop constructs. When encountered, break causes the program to exit the current block and continue execution after it.
5. case
Used in switch statements to evaluate different condition types.
6. catch
catch is used with try statements for exception handling.
7. char
char (character) type, a fundamental data structure in C++ typically holding values from 0 to 255, corresponding to the 256 ASCII codes. Char data must be enclosed in single quotes '
.
8. class
class is the foundation of C++ object-oriented design. It is used to declare a class.
9. const
const (constant) indicates that the object or variable cannot be altered. When used to modify a function, it means the function cannot change any variables declared outside the function or call any non-const functions. It must be applied in both the declaration and definition of the function, right after the parameter list. In C++, const variables are typed constants that can replace #define, providing type information and internal linkage, allowing them to be declared in header files.
10. const_cast Usage:
const_cast<type_id> (expression)
This operator modifies the const or volatile attributes of a type. It converts a constant pointer to a non-constant pointer, still pointing to the original object, and similarly for constant references and objects.
11. continue
continue (continue) keyword is used in loop structures to skip the remaining part of the current iteration and start a new one, unlike break which exits the loop.
12. default
default (default, omitted) is used in switch statements. It executes if none of the cases match. It must follow all cases and is optional.
13. delete
delete (delete) frees dynamically allocated memory in the program. It is typically followed by a pointer or array [] and can only delete pointers allocated with new, otherwise a segmentation fault occurs.
14. do
do-while is a loop structure that ensures the loop body executes at least once, unlike a while loop.
15. double
double (double precision) type, a fundamental data structure in C++ for storing floating-point numbers in double precision.
16. dynamic_cast
dynamic_cast (dynamic cast) allows type conversion at runtime, enabling safe type casting within a class hierarchy. It supports converting base class pointers to derived class pointers and base class lvalues to derived class references.
17. else
else follows an if statement to handle the case where the if condition is not met.
18. enum
enum (enumeration) type defines a set of fixed values from which one can be chosen.
19. explicit
explicit (explicit) prevents the single-argument constructor from being used for implicit type conversion, particularly useful for container types where the initial size can be passed to the constructor.
20. export
export (export) is used for accessing variables or objects from other translation units (like other code files) for template types. It must be used when defining template class objects and functions.
21. extern
extern (external) declares variables or functions as external linkage, visible in other files. It statically allocates space for variables at the start of the program and releases it at the end. It should be used for variables or functions defined elsewhere.
22. false
false (false), one of the values of the bool type, equivalent to the int value 0.
23. float
float (floating-point number), a fundamental data structure in C++ with precision less than double.
24. for
for is one of the loop structures in C++.
25. friend
friend (friend) declares a friend relationship, allowing access to private/protected members of a class for efficiency. It includes friend functions and friend classes.
26. goto
goto (go to) performs an unconditional jump to a labeled location.
27. if
if (if), one of the conditional statements in C++, executes a branch based on a bool value.
28. inline
inline (inline) functions are expanded at the point of call during compilation, typically used for short functions to improve efficiency.
29. int
int (integer), a fundamental data structure in C++ for representing integers, with precision less than long.
30. long
long (long integer), a fundamental data structure in C++ for representing long integers.
31. mutable
mutable (mutable) is a rarely used keyword in C++ for non-static and non-const class members. It allows modification of class members within const member functions.
32. namespace
namespace (namespace) organizes classes logically, larger than a class.
33. new
new (new) creates a new object and always returns a pointer.
34. operator
operator (operator) is used for operator overloading, a special function in C++.
35. private
private (private), an access control specifier in C++. Private members can only be accessed within the class and its friends.
36. protected
protected (protected), an access control specifier in C++. Protected members can be accessed within the class, its derived classes, and friends.
37. public
public (public), an access control specifier in C++. Public members can be accessed by any class.
38. register
register (register) declares variables as register variables, which may be stored in a machine register for faster access. This is less effective with 32-bit compilers and depends on global optimizations.
39. reinterpret_cast
Usage:
reinterpret_cast<type-id> (expression)
type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer to an integer and vice versa, preserving the original pointer value.
40. return
return (return) is used to return a value from a function. Execution stops at the return statement, and subsequent statements are not executed.
41. short
short (short integer), a fundamental data structure in C++ for representing integers with precision less than int.
42. signed
signed (signed) indicates that the type is a signed number, opposite to unsigned. Numeric types (integer and floating-point) can be signed, but they are by default, so it's rarely explicitly used.
43. sizeof
sizeof operator determines the size in bytes of a data type, enhancing portability as sizes are determined by the compiler.
44. static
static (static) variables have file scope, are allocated at program start, deallocated at end, default to 0, and can have their values changed. They are only accessible within the file and are known as "file scope." Static member variables in a class are shared among all instances, and static member functions can only access static members.
45. static_cast
Usage:
static_cast < type-id > ( expression )
This operator converts expression to type-id without runtime type checks. It is used for:
- Converting pointers or references between base and derived classes (upcasting is safe, downcasting is not).
- Converting between basic data types, like int to char.
- Converting null pointers to target type null pointers.
- Converting any type to void.
46. struct
struct (structure) type, similar to class, compatible with C language (class is not). It supports object-oriented programming.
47. switch
The switch
statement is similar to the if-else-if
statement and is a multi-branch statement. It provides a concise way of writing and can generate more efficient code. However, the condition following switch
can only be of type int
(or char
, but char
is essentially also an int
type). The default
branch at the end of the switch
statement is optional.
48. template
template
is the implementation of generic mechanisms in C++.
49. this
this
returns a pointer to the caller itself.
50. throw
throw
is used to implement C++'s exception handling mechanism, allowing an exception to be "thrown" with the throw
keyword.
51. true
true
is one of the values of the bool
type in C++, equivalent to a non-zero value of int
.
52. try
try
is used to implement C++'s exception handling mechanism. Functions that may throw exceptions can be called within try
, and then caught and handled in the subsequent catch
.
53. typedef
typedef
(type define) has the format:
typedef type newname;
It specifies a new name for a data type, not defining a new data type. The newname represents the new name for this type.
54. typeid
Indicates the actual derived type of the object pointed to or referenced.
55. typename
The typename
keyword tells the compiler to interpret a special name as a type. It must be used with a name in the following cases:
- A unique name (which can be understood as a type) nested within another type.
- Dependent on a template parameter, meaning the template parameter somehow includes this name. This is necessary when the template parameter causes the compiler to misinterpret a type.
56. union
union
is similar to enum
. However, enum
is essentially of int
type, whereas union
can be used for all types, and its size changes according to the actual type.
57. unsigned
unsigned
indicates that the type is an unsigned number, opposite to signed
.
58. using
Indicates the use of a namespace.
59. virtual
virtual
is used in C++ to implement polymorphism.
60. void
void
can be used as a function return type to indicate no return value; as a parameter to indicate no parameters are passed (not mandatory in C++); and as a pointer.
61. volatile
volatile
specifies that an object can be changed by an external process (operating system, hardware, or concurrent threads, etc.). The declaration syntax is as follows:
int volatile nVint;
Such declarations are not optimized to the highest efficiency because their values can change at any time, requiring the system to frequently read and write the value of this object. Therefore, they are often used for memory access in asynchronous processes like interrupt handlers.
62. wchar_t
wchar_t
is a wide character type, with each wchar_t
occupying 2 bytes, 16 bits wide. Chinese characters are represented using wchar_t
.