sizeof不是函数,是操作符,是关键字
来源:互联网 发布:移动宽带网络机顶盒 编辑:程序博客网 时间:2024/05/16 08:39
1、
#include <stdio.h>
int main(){
int i;
i =10;
printf("i : %d\n",i);
printf("sizeof(i++) is: %d\n",sizeof(i++));
printf("i : %d\n",i);
return0;
}
参考答案:如果你觉得输出分别是,10,4,11,那么你就错了,错在了第三个,第一个是10没有什么问题,第二个是4,也没有什么问题,因为是32位机上一个int有4个字节。但是第三个为什么输出的不是11呢?居然还是10?原因是,sizeof不是一个函数,是一个操作符,其求i++的类型的size,这是一件可以在程序运行前(编译时)完全的事情,所以,sizeof(i++)直接就被4给取代了,在运行时也就不会有了i++这个表达式。
2、
char *string1="hello world";
char string2[]="hello world";
cout<<"sizeof(string1)="<<sizeof(string1)<<endl; //输出4,string1是指针
cout<<"sizeof(string2)="<<sizeof(string2)<<endl; //输出12,stirng2是数组
3、
在c++中
如果一个类中没有数据成员,并且没有虚函数(即没有指针),则用sizeof测试这个类的大小为1(VS2010编译器);如果有虚函数则测试结果为4。示例如下:
#include<iostream>#include<complex>using namespace std;class Base{public:Base(){cout<<"Base-ctor"<<endl;}~Base(){cout<<"Base-dtor"<<endl;}//virtual void f(int)//{//cout<<"Base""F(int)"<<endl;//}//virtual void f(double)//{//cout<<"Base::f(double)"<<endl;//}virtual void g(int i=10){cout<<"Base:g()"<<endl;}void g2(int i=10){cout<<"Base::g2()"<<endl;}};class Derived:public Base{public:Derived(){cout<<"Derived-ctor"<<endl;}~Derived(){cout<<"Derived-dtor"<<endl;}void f(complex<double>){cout<<"Derived::f(complex)"<<endl;}//virtual void g(int i=20)//{//cout<<"Derived::g()"<<i<<endl;//}};int main(){Base b;Derived d;//Base *pb = new Derived;cout<<sizeof(Base)<<" tt"<<endl;cout<<sizeof(Derived)<<" bb"<<endl;return 0;}如果把Base里面的虚函数全部注释掉,则sizeof(Base)=1。只有Base里面有虚函数,则sizeof(Base)=4
- sizeof不是函数,是操作符,是关键字
- sizeof 是关键字不是函数
- sizeof不是函数,是操作符
- sizeof既是关键字,又是运算符(操作符),但不是函数!
- c语言:sizeof的使用,其不是函数,是一个操作符关键字,定义一个数组arr,输出arr和&arr的区别
- [C] sizeof居然不是函数是运算符
- sizeof()是一个函数!!!不是宏
- C语言sizeof是关键字还是函数?
- sizeof 是一个关键字
- 一句话经验:sizeof是操作符而不是函数
- sizeof:“我是关键字,不是函数!!!”
- 关键字----sizeof(不是函数)
- sizeof 是运算符而不是关键字
- sizeof是关键字还是运算符
- javascript typeof 是个操作符 不是个函数
- sizeof到底是关键字还是运算符呢?
- 怎样证明sizeof()是一个运算符,而不是函数
- sizeof--常年被人误认为是函数
- 如何在Linux系统下制作RamDisk
- Linux 汇编语言开发指南
- ado连接sql server怎么使用recordcount?
- 三进制与两道趣味数学题
- MyEclipse配置Tomcat最详细的图解教程
- sizeof不是函数,是操作符,是关键字
- 解决Adobe Master Collection序列号过期问题(CS5 expired Solution)
- 分析C语言的声明
- 数据挖掘十大经典算法
- 如何在链表中释放元素
- 用AutoCompleteTextView实现历史记录提示
- qsort的使用
- JSPServlet精华笔记—新手必备
- Mysql删除foreign key 和 key