Something Conclusion in C++
Object
- Most generally, an object is a region of memory that has type.
const Qualifier
- "const type object" means object's type is type, and object is const. 由内而外的解析.
const qualifier
int ivar = 2;
typedef int *PI;
// the type of pi is PI = (int*), and pi is const
const PI pi = &ivar;
// pi is a pointer, the type of (*pi) is int, and (*pi) is const
const int *pi;
// pi is const, pi is a pointer, the type of (*pi) is int, and (*pi) is const
const int *const pi = &ivar;
- const reference
- Names defined outside any functions have global scope.
When we define a non-const variable at global scope, it is accessible throughout the program. Unless specified using extern, const variables declared at global scope are local to a file by default. (So const variables can be defined in header files.)
- const member function
That 'const' modifies the type of the implicit 'this' parameter.
const member function
class A {
public:
int m_ivar;
A():m_ivar(1024) {}
~A(){}
int get() const {
return m_ivar;
}
};
int main()
{
A insA;
// insA.get( const A *const this)
insA.get();
return 0;
}
这样也就解释了:成员函数的重载需要考虑const或volatile,因为这样改变了隐藏参数this的类型。
array
- sizeof 返回一个 ‘对象的类型’ 或 ‘类型名’ 的长度,长度的单位是字节数,sizeof表达式的结果是编译时的常量。