????Const?????????????
????const??????????????????????α??????????const??????????????????Щ???????
?????????????????????????????????????????‘*’??????????????????????????????????????????????????????????????
????const int *p1; // p1 is a non-const pointer and points to a const int
????int * const p2; // p2 is a const pointer and points to a non-const int
????const int * const p3; // p3 is a const pointer and points to a const it
????const int *pa1[10]; // pa1 is an array and contains 10 non-const pointer point to a const int
????int * const pa2[10]; // pa2 is an array and contains 10 const pointer point to a non-const int
????const int (* p4)[10]; // p4 is a non-const pointer and points to an array contains 10 const int
????const int (*pf)(); // pf is a non-const pointer and points to a function which has no arguments and returns a const int
????...
????const?????????????????Щ??……
????????????const??????????????и????????????const????????????????и????????????????????????????const???????const????????const Type * const???
????????const??????????????????????const?????const?????????????????const?????????????????????????????????const???岻???????
??????????const_cast?????????????????const???????????static_cast????????const???????ü???const?????
????int i;
????const int *cp = &i;
????int *p = const_cast<int *>(cp);
????const int *cp2 = static_cast<const int *>(p); // here the static_cast is optional
????C++???е?this?????????????const??????????const?????е?this????????????????const?????
???????е?const???????
???????е?const??????????????????static??????static??????
??????static??????
???????е??static??????????????????????б??н??г????????????е??static???????????????????????????????????const?????????????????????????????????????????
????class B {
????public:
????B(): name("aaa") {
????name = "bbb"; // !error
????}
????private:
????const std::string name;
????};
????static??????
????static?????????????????????????????????е??????????????????????????.cpp?а??????static????????壺
????// a.h
????class A {
????...
????static const std::string name;
????};
????// a.cpp
????const std::string A::name("aaa");
?????????????????static????????????????????????????char??int??size_t????????????????????????????????????????????ж??????????????????static????????滻?????????????????滻??????????????????????????????????????static??????????????????????????????????????????????????????????????????????壬???????????????????????????????????
????// a.h
????class A {
????...
????static const int SIZE = 50;
????};
????// a.cpp
????const int A::SIZE = 50; // if use SIZE as a variable?? not a macro
????const???κ???
????C++?п?????const????????????static????????????????????ú????????????????const?????const????????У????п???Υ??this???const???const????????е?this?????????const????????????????????????????????????????????????const?????????????????const??????????????????mutable???????????????κβ????????????????????????????????????
????????const???κ??????????????????????У??????const????const??????????÷?????????????const??????
class A {
public:
int &operator[](int i) {
++cachedReadCount;
return data[i];
}
const int &operator[](int i) const {
++size; // !error
--size; // !error
++cachedReadCount; // ok
return data[i];
}
private:
int size;
mutable cachedReadCount;
std::vector<int> data;
};
A &a = ...;
const A &ca = ...;
int i = a[0]; // call operator[]
int j = ca[0]; // call const operator[]
a[0] = 2; // ok
ca[0] = 2; // !error
????????????У?????????汾??operator[]??????????????????????????????????????????????????????????????????ο?Effective C++????????????????÷?const?汾?????const?汾??
????int &A::operator[](int i) {
????return const_cast<int &>(static_cast<const A &>(*this).operator[](i));
????}
??????????????????????????????????????*this????const A &?????????static_cast????ɡ?????????const operator[]?????????????????????const?????????const_cast????ɡ???????const_cast????????????????????????????????????????????const?????????????const_cast???????
????constexpr
????constexpr??C++11?????????????????????“????????”???????????????????????????????????????????????????/??????????sizeof??????????????????????????????????????????????????????????????constexpr???????enum??switch?????鳤???????
????constexpr?????ε??????????????????????????ε???????????в???????constexpr??????????constexpr??
????constexpr int Inc(int i) {
????return i + 1;
????}
????constexpr int a = Inc(1); // ok
????constexpr int b = Inc(cin.get()); // !error
????constexpr int c = a * 2 + 1; // ok
????constexpr???????????????????????????????????ù??????????????constexpr???????????????е????г????????constexpr??????????constexpr??????????????????????constexpr?????????constexpr??????????????????????壬?????г???????????????????????б??С?
????struct A {
????constexpr A(int xx?? int yy): x(xx)?? y(yy) {}
????int x?? y;
????};
????constexpr A a(1?? 2);
????enum {SIZE_X = a.x?? SIZE_Y = a.y};
????constexpr??????
????????????????????????????????????岻???????
??????????????????????constexpr???????з?????????????罫?????constexpr??????????滻?????????
???????????????ж?????????????????????