惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
博客园 - 聂微东
小众软件
小众软件
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
TaoSecurity Blog
TaoSecurity Blog
博客园 - 司徒正美
罗磊的独立博客
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
S
Security @ Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
月光博客
月光博客
S
Secure Thoughts
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
H
Heimdal Security Blog
W
WeLiveSecurity
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
L
LangChain Blog
T
The Blog of Author Tim Ferriss
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Project Zero
Project Zero
SecWiki News
SecWiki News
爱范儿
爱范儿
The Register - Security
The Register - Security
AI
AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
L
Lohrmann on Cybersecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Privacy International News Feed
J
Java Code Geeks
S
Securelist
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Visual Studio Blog

博客园 - cacard

Android中的内部类引起的内存泄露 Android的消息机制: Message/MessageQueue/Handler/Looper ArrayList/Vector的原理、线程安全和迭代Fail-Fast JVM中的Stack和Frame JVM中的垃圾收集算法和Heap分区简记 无锁编程以及CAS 简述Java内存模型的由来、概念及语义 MQTT协议简记 RabbitMQ的工作队列和路由 RabbitMQ 入门 [Java] LinkedHashMap 源码简要分析 [Java] HashMap 源码简要分析 [Java] Hashtable 源码简要分析 CentOS 安装 Hadoop 手记 树莓派(RespberryPi)安装手记 RPC简述 [C++] 左值、右值、右值引用 [C++] 引用 Java线程池 / Executor / Callable / Future
[C++] const与重载
cacard · 2013-09-11 · via 博客园 - cacard

下面的两个函数构成重载吗?

void M(int a){} //(1)
void M(const int a){} //(2)

下面的呢?

void M(int& a){} //(3)
void M(const int& a){} //(4)

const在函数中的含义是该值在此函数范围内“无法修改”。站在调用者的角度,所有的值传递都是无法修改实参的。所以,(1)/(2)两个函数在调用者看来,是语义相同的,不能构成重载。

(4)与(3)的区别是,(4)无法修改引用指向的对象,而(3)可以。从调用者的角度,两个函数有不同的语义,构成重载。

demo 

 #include <iostream>
 using namespace std;

 class Y{};

 /* 下面两个函数具有相同语义,即a均是值拷贝,无法改变实参。 */
 void Method1(int a){}
 void Method1(const int a){} // error:redefinition

  /* 同样的语义,对象拷贝 */
 void Method2(Y y){}
 void Method2(const Y y){} // error:redefinition

 /* 下面两个函数具有不同语义,即后者无法改变实参,之所以使用引用,可能是因为不想拷贝,节省内存。 */
 void Method3(int& a){}
 void Method3(const int& a){}

 void Method4(Y& y){}
 void Method4(const Y& y){}

 int main(int count,char * args[])
 {
     return 0;
 }