📄 safecons.cpp
字号:
//: C08:Safecons.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Using const for safety
#include <iostream>
using namespace std;
const int i = 100; // Typical constant ,i is a compile-time const
const int j = i + 10; // Value from const expr,j still is a compile-time const
long address = (long)&j; //requires the address of j and therefore forces the compiler to allocate storage for j.
char buf[j + 10]; // Still a const expression
void main() {
cout<<i<<endl;
cout<<j<<endl; //设置断点,察看i,j是否已分配存储?
// It’s possible to use const for aggregates,so storage will be allocated. In these situations, const means “a piece of storage that cannot be changed.”
const int i[] = { 1, 2, 3, 4 };
float f[i[3]]; // Illegal //However, the value cannot be used at compile time because the compiler is not required to know the contents of the storage at compile time.
struct S { int i, j; };
const S s[] = { { 1, 2 }, { 3, 4 } };
double d[s[1].j]; // Illegal
// ...
} ///:~
/*
//file1
extern float j; //external linkage
const int k=4; //internal linkage
//extern const int k=4;//explicitly define k as external linkage
void main()
{
float a = j*k;
}
float j=(float)2.2; //definition
//file2
extern float j; // declaration
//extern const int k;
void fun() //definition
{
j=10*k;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -