1、什么是sizeof
首先看一下sizeof在msdn上的定义:
The sizeof keyWord gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.
看到return这个字眼,是不是想到了函数?错了,sizeof不是一个函数,你见过给一个函数传参数,而不加括号的吗?sizeof可以,所以sizeof不是函数。网上有人说sizeof是一元操作符,但是我并不这么认为,因为sizeof更像一个非凡的宏,它是在编译阶段求值的。举个例子:
coutsizeof(int)endl; // 32位机上int长度为4
coutsizeof(1==2)endl; // == 操作符返回bool类型,相当于 coutsizeof(bool)endl;
在编译阶段已经被翻译为:
cout4endl;
cou...[ 查看全文 ]