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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 撬棍

【.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 的指针的数组。