<P>  void fun() const{};    、 const void fun(){}; 和void const fun(){}; 的区别?</P>
<P>  答:const void fun(){};和void const fun(){};两个相同
。</P>
<P>  如果采用"按址传递方式"的函数返回值加const 修饰
,那么函数返回值(即地址)的内容不能被修改
,该返回值只能被赋给加const 修饰的同类型指针
。</P>
<P>  如果采用"按值传递方式"的函数返回值加const 修饰,由于函数会把返回值复制到外部临时的存储单元中,加const 修饰没有任何价值。</P>
<P>  所以不要尽量不要把int fun2();写成const int fun2(); 因为没意义。</P>
<P>  例:</P>
<P>  #include<iostream></P>
<P>  using namespace std;</P>
<P>  int num=10;    //全局变量</P>
<P>  const int *fun1(){    //按址传递</P>
<P>  return &num;        //返回地址</P>
<P>  }</P>
<P>  const int fun2(){     //按值传递 //最好直接写int fun2()</P>
<P>  return num;</P>
<P>  }</P>
<P>  int main()</P>
<P>  {</P>
<P>  const int *fun1();</P>
<P>  //         int *t1=fun1(); //错误,必须是const型</P>
<P>  const int *t1=fun1();</P>
<P>  //         *t1=20;    //按址传递,不能修改其指向变量或常量的值</P>
<P>  cout<<"const int *fun1() :	"<<*t1<<endl;</P>
<P>  const int fun2(); //最好直接声明成int fun2()</P>
<P>  int t2=fun2();    //非const变量可以更改函数返回值</P>
<P>  const int t3=fun2();</P>
<P>  t2 += 10;    //按值传递,可以修改返回值</P>
<P>  cout<<"const int fun2() :	"<<t2<<endl;</P>
<P>  return 0;</P>
<P>  }</P>
<P>  void fun() const{};</P>
<P>  类的成员函数后面加 const,表明这个函数不可以对这个类对象的数据成员(准确地说是非static数据成员)作任何改变。</P>
<P>  例:</P>
<P>  #include<iostream></P>
<P>  using namespace std;</P>
<P>  class R</P>
<P>  {</P>
<P>  public:</P>
<P>  R():num1(1){}</P>
<P>  int sum1(int a)const</P>
<P>  {</P>
<P>  // num1=10; //错误,不可以修改非static数据成员</P>
<P>  return a+num1;</P>
<P>  }</P>
<P>  int sum2(int a)const</P>
<P>  {</P>
<P>  num2=2;    //正确,修改static数据成员</P>
<P>  return a+num2;</P>
<P>  }</P>
<P>  int sum3(int a)    //没有const</P>
<P>  {</P>
<P>  num1=10; //正确,修改非static数据成员</P>
<P>  num2=20; //正确,修改static数据成员</P>
<P>  return a+num1+num2;</P>
<P>  }</P>
<P>  private:</P>
<P>  int num1;</P>
<P>  static int num2;</P>
<P>  };</P>
<P>  int R::num2=0;</P>
<P>  int main()</P>
<P>  {</P>
<P>  cout<<"t.sum1(1):	"<<t.sum1(1)<<endl;</P>
<P>  cout<<"t.sum2(1):	"<<t.sum2(1)<<endl;</P>
<P>  cout<<"t.sum3(1):	"<<t.sum3(1)<<endl;</P>
<P>  return 0;</P>
<P>  }</P>