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. LeetCode解题报告—— N-Queens && Edit Distance

    1. N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  2. centos 7 防火墙设置

    一.介绍 centos 7 的防火墙是以firewalld daemon的形式存在,区别于iptables 二.使用方法 centos7 主要通过firewall-cmd命令来管理firewall, ...

  3. JSON-lib的api的使用

    List<Object> list = new ArrayList<Object>(); Map<String,Object> map1 = new HashMap ...

  4. [解决] python WindowsError: [Error 3]

    python 运行出错 WindowsError: [Error ] : 'D:\\\xe7\x81\xab\xe8\xbd\xa6\xe9\x87\x87\xe9\x9b\x86\xe5\x99\x ...

  5. 【转载】AsyncTask源码分析

    原文地址:https://github.com/white37/AndroidSdkSourceAnalysis/blob/master/article/AsyncTask%E5%92%8CAsync ...

  6. 动若脱兔:深入浅出angr--初步理解符号执行以及angr架构

    一:概论 angr作为符号执行的工具,集成了过去的许多分析方式,它不仅能进行动态符号执行,而且还能进行很多静态分析,他在分析二进制程序中能发挥很大的作用,下面为一些应用: 1:利用符号执行探究执行路径 ...

  7. ARP扫描工具arp-scan

    ARP扫描工具arp-scan   arp-scan是Kali Linux自带的一款ARP扫描工具.该工具可以进行单一目标扫描,也可以进行批量扫描.批量扫描的时候,用户可以通过CIDR.地址范围或者列 ...

  8. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

  9. Socket读取页面

    http://www.knowsky.com/363189.html http://hi.baidu.com/myyers/item/f90fa3f57d89e1d243c36a34 http://h ...

  10. [Codeforces #494] Tutorial

    记录下一开始写错的两道水题 E: 先建出直径,然后在保证直径不变的情况下按照最大度数贪心就好了 注意一下一开始的特判 #include <bits/stdc++.h> using name ...