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

推荐订阅源

NISL@THU
NISL@THU
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Cloudbric
Cloudbric
H
Heimdal Security Blog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
A
Arctic Wolf
W
WeLiveSecurity
SecWiki News
SecWiki News
S
Security Affairs
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
T
The Exploit Database - CXSecurity.com
V2EX - 技术
V2EX - 技术
AI
AI
Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
有赞技术团队
有赞技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 聂微东
H
Hacker News: Front Page
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
月光博客
月光博客
博客园 - 【当耐特】
Recorded Future
Recorded Future
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
Scott Helme
Scott Helme
N
News and Events Feed by Topic
Help Net Security
Help Net Security
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
小众软件
小众软件

博客园 - 撬棍

【.Net】2、8、16进制转换 【.Net】执行CMD命令 【.Net】获取随机数函数 【.Net】注册程序开机启动 【.Net】把窗体“钉”到桌面上 【.Net】多语言查看MSDN 【.Net】 显示星期字符串 【.Net】 判断时间字符串正确性 【.Net】 实现窗口拖动 【.Net】 Winform 单例运行实例 [C++]函数返回值 [C++]const的指针使用 [C++]指针类型出参 [VBA]Excel输出utf-8编码格式文件 使用WideCharToMultiByte 【C++】split [C语言学习]之打印万年历 - 撬棍 - 博客园 [VB6.0]让程序在任务列表和资源管理器“隐身” [InstallShield]FindAllFiles与SetFileInfo配合实现文件加多文件属性设置 [VB]修改注册表让程序开机自动运行 - 撬棍 - 博客园
[C++]数组参数
撬棍 · 2013-05-14 · via 博客园 - 撬棍

在C++中数组永远不会按值传递它是传递第一个元素准确地说是第0 个的指针。

例如如下声明

void putValues( int[ 10 ] );

被编译器视为

数组的长度与参数声明无关因此下列三个声明是等价的

  // 三个等价的putValues()声明
  void putValues( int* );
  void putValues( int[] );
  void putValues( int[ 10 ] );

第一种是传指针不用说了。

另外一种机制是将参数声明为数组的引用当参数是一个数组类型的引用时数组长度,
成为参数和实参类型的一部分编译器检查数组实参的长度与在函数参数类型中指定的长度是否匹配。--可能也正是因为这种限制很少使用此种方式。

// 参数为10 个int 的数组
// parameter is a reference to an array of 10 ints
void putValues( int (&arr)[10] );
int main() {
    int i, j[ 2 ];
    putValues( i ); // 错误: 实参不是 10 个 int 的数组
   putValues( j ); // 错误: 实参不是 10 个 int 的数组
   return 0;
}

因为数组的长度现在是参数类型的一部分所以putValues()的这个版本只接受10 个int的数组。

void putValues( int (&ia)[10] )
{
    std::cout << "( 10 )< ";
    for ( int i = 0; i < 10; ++i ) 
    {
        std::cout << ia[ i ];
        // 用逗号分隔元素
        if ( i != 9 )
            std::cout << ", ";
    }
    std::cout << " >\n";
}
void main()
{
    int i[10]  = {1,2,3,4,5,6,7,8,9,0};
    putValues( i ); // 错误: 实参不是 10 个 int 的数组
    return;
}

注意:*ia周围的括号是必需的因为下标操作符的优先级较高下列声明:

将ia声明成一个含有10 个指向int 的指针的数组。