void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别?
  const void fun(){};和void const fun(){};两个相同。
  如果采用"按址传递方式"的函数返回值加const 修饰,那么函数返回值(即地址)的内容不能被修改,该返回值只能被赋给加const 修饰的同类型指针。
  如果采用"按值传递方式"的函数返回值加const 修饰,由于函数会把返回值复制到外部临时的存储单元中,加const 修饰没有任何价值。
  所以不要尽量不要把int fun2();写成const int fun2(); 因为没意义。
  例:
[csharp] view plaincopy
#include<iostream>
using namespace std;
; //全局变量
const int *fun1(){ //按址传递
return &num; //返回地址
}
const int fun2(){ //按值传递 //最好直接写int fun2()
return num;
}
int main()
{
const int *fun1();  

// int *t1=fun1(); //错误,必须是const型
const int *t1=fun1();
// *t1=20; //按址传递,不能修改其指向变量或常量的值
cout<<"const int *fun1() :\t"<<*t1<<endl;
const int fun2(); //最好直接声明成int fun2()
int t2=fun2(); //非const变量可以更改函数返回值
const int t3=fun2();
t2 += ; //按值传递,可以修改返回值
cout<<"const int fun2() :\t"<<t2<<endl;
;
}
  void fun() const{};
  类的成员函数后面加 const,表明这个函数不可以对这个类对象的数据成员(准确地说是非static数据成员)作任何改变例:
[csharp] view plaincopy
#include<iostream>
using namespace std;
class R
{
public:
R():num1(){}
int sum1(int a)const
{
// num1=10; //错误,不可以修改非static数据成员
return a+num1;
}
int sum2(int a)const
{
num2=; //正确,修改static数据成员
return a+num2;  

}
int sum3(int a) //没有const
{
num1=; //正确,修改非static数据成员
num2=; //正确,修改static数据成员
return a+num1+num2;
}
private:
int num1;
static int num2;
};
;
int main()
{
cout<<)<<endl;
cout<<)<<endl;
cout<<)<<endl;
;
}  

http://bbs.csdn.net/topics/350148926

http://bbs.csdn.net/topics/340217434

void fun() const{}; const void fun(){}; 和void const fun(){}; 的区别?的更多相关文章

  1. (转)Const,Const函数,Const变量,函数后面的Const

    本文转自http://www.cnblogs.com/Fancyboy2004/archive/2008/12/23/1360810.html 看到const 关键字,C++程序员首先想到的可能是co ...

  2. const char*、char*、char* const、char[]、string的区别

    1.const char* p: p is a pointer to const char(char const* p 一样)   意思就是不能通过p指针来修改p指向的内容(但是内容可以修改). 2. ...

  3. [EffectiveC++]item03:尽可能使用const 并且转载一篇关于const函数的博客

    速度 #include <iostream> using namespace std; class TextBlock { private: string text; public: Te ...

  4. C#中的静态常量(const)和动态常量(static和readonly)用法和区别

    C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景.工作原理 readonly为运行时常量,程序运行时进行赋 ...

  5. iOS—— static和const联合使用;使用static const 与 #define

    static和const联合使用:   static将一个全局变量变成局部变量   const将一个局部变量变成局部常量 // 定义了一个局部常量      static const CGFloat ...

  6. iOS-static和const联合使用;使用static const 与 #define

    static和const联合使用:   static将一个全局变量变成局部变量   const将一个局部变量变成局部常量 // 定义了一个局部常量      static const CGFloat ...

  7. a href="javascript:void(0)" 是什么意思?加不加上有什么区别?

    <A href="javascript:void(0)">点击</a>点击链接后不会回到网页顶部<A href="#">点击 ...

  8. void f(int(&amp;p)[3]){} 和void f(int(*p)[3]){}的差别

    #include<iostream> using namespace std; void f(int(&p)[3]){          cout<<p[0]<& ...

  9. void(*p)()和void*p()区别

    void (*p)()是一个指向函数的指针,表示是一个指向函数入口的指地变量,该函数的返回类型是void类型.它的用法可参看下例: 例如:有一返加void值的函数swap,(swap用来交换两个数) ...

  10. C++中的 CONST 含义(从#define 到 CONST 的转变)

    const 与define 两者都可以用来定义常量,但是const定义时,定义了常量的类型,所以更精确一些.#define只是简单的文本替换,除了可以定义常量外,还可以用来定义一些简单的函数,有点类似 ...

随机推荐

  1. thinkphp5 消息队列thinkphp-queue扩展

    1.简介 thinkphp-queue是thinkphp的一个第三方扩展, 内置了 Redis,Database,Topthink ,Sync这四种驱动,推荐使用redis 2. 下载 和安装 com ...

  2. linux命令(41):watch命令

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  3. 阿里云轻量应用服务器——配置MySQL远程连接(踩坑,LAMP+CentOS)

    说在前面 本文讲解清晰,从0开始 如不能用Navicat等数据库软件远程登陆,请先检查:安全>防火墙中 是否添加了MYSQL的3306端口(ECS服务器请检查 安全组)如未添加,先点右上角“添加 ...

  4. git reset用法

    git 删除 错误 提交的 commit 方法:         根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:    git reset -- ...

  5. hdu5782

    官方题解不是很详细 首先有一个结论:若A=pa+sa B=pb+sb A.B串循环同构,则可以构造一个可行方案(pa,sb) (sa,pb)中有一个是最长匹配,这个不难用反证法证明. 对于s1,s2串 ...

  6. [Bootstrap]modal弹出框

    写在前面 在实际开发中,为了友好,更需要一种美观的弹出框,js原生的alert,很难满足需求.这里推荐一个bootstrap的弹出框. 一个例子 先看效果吧 代码: <!DOCTYPE html ...

  7. git 命令小结

    一.git 版本管理 1.git log: 获取当前版本之前的所有操作 2.git log --pretty=oneline:获取当前版本的前三和后三个操作 3.git reflog :获取当前项目下 ...

  8. Java中面向对象的分拣存储

    Student.java package yzhou.map; /** * 学生类 * * * @author 洋 * */ public class Student { private String ...

  9. centos7中安装wdcp管理系统(用于网站搭设)

    首先我们进入官网看下安装方法https://www.wdlinux.cn/wdcp/install.html 可以看到,实际上有两张安装方式,一种是源码进行安装,还有一种是RPM包安装,显然第二种安装 ...

  10. Did Pong Lie? (差分系统 判负环)

    Did Pong Lie? 时间限制: 5 Sec  内存限制: 128 MB提交: 68  解决: 15[提交][状态][讨论版] 题目描述 Doctor Pong has two arrays o ...