gpt4 book ai didi

c++ - 函数原型(prototype)作用域示例

转载 作者:行者123 更新时间:2023-11-30 00:50:37 25 4
gpt4 key购买 nike

有一个函数原型(prototype)作用域的定义(3.3.4/1 N3797):

In a function declaration, or in any function declarator except the declarator of a function definition (8.4), names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.

我们可以举个例子来描述这个规则吗?

最佳答案

这是一个简单的例子

int a;

void f( int a, int a );

编译器将对第二个参数 a 发出错误,因为它的名称与第一个参数的名称一致。即编译器会报告名称a 有重定义。同一个名字在同一个范围内定义了两次。

或者另一个例子

struct A {};

void f( int A, struct A );

第一个参数名称隐藏了结构名称,因此第二个参数使用结构的详细名称定义。

关于c++ - 函数原型(prototype)作用域示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24393273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com