- What Is a Variable?
- Common Compiler-Supported C++ Variable Types
- Determining the Size of a Variable by Using sizeof()
- Automatic Type Inference Using auto
- Using typedef to Substitute a Variable’s Type
- What Is a Constant?
- Keywords You Cannot Use as Variable or Constant Names
- Summary
- Q&A
- Workshop
Keywords You Cannot Use as Variable or Constant Names
Some words are reserved by C++, and you cannot use them as variable names. Keywords have special meaning to the C++ compiler. Keywords include if, while, for, and main. A list of keywords defined by C++ is presented in Table 3.2, as well as in Appendix B, “C++ Keywords.” Your compiler might have additional reserved words, typically documented in its manuals.
TABLE 3.2 Major C++ Keywords and Reserved Words
Keywords |
|||
---|---|---|---|
asm |
else |
new |
this |
auto |
enum |
operator |
throw |
bool |
explicit |
private |
true |
break |
export |
protected |
try |
case |
extern |
public |
typedef |
catch |
false |
register |
typeid |
char |
float |
reinterpret_cast |
typename |
class |
for |
return |
union |
const |
friend |
short |
unsigned |
constexpr |
goto |
signed |
using |
continue |
if |
sizeof |
virtual |
default |
inline |
static |
void |
delete |
int |
static_cast |
volatile |
do |
long |
struct |
wchar_t |
double |
mutable |
switch |
while |
dynamic_cast |
namespace |
template |
|
Reserved Words |
|||
and |
bitor |
not_eq |
xor |
and_eq |
compl |
or |
xor_eq |
bitand |
not |
or_eq |
DO |
DON’T |
---|---|
DO give variables descriptive names, even if doing so makes them long. DO check whether your team is following certain naming conventions and follow them. DO initialize variables: Use list initialization to avoid narrowing conversion errors. |
DON’T give variables names that are too short or that contain just a character. DON’T give variables names that use exotic acronyms known only to you. DON’T give variables names that are reserved C++ keywords as they won’t compile. |