原型声明和正向声明之间的区别? [英] Difference between prototype declaration and forward declaration?

查看:42
本文介绍了原型声明和正向声明之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码:

class xx
{
    int getnum(); //Is this a forward declaration or a prototype declaration and why?
};

int xx::getnum()
{
    return 1+3;
}

因此,该问题已在代码中被注释,但是:

So the question has already been commented in the code but:

int getnum(); 是前向声明还是原型声明,为什么?

Is int getnum(); a forward declaration or a prototype declaration and why?

推荐答案

C ++标准中都没有定义术语前向声明"或原型声明",因此从严格意义上讲,两者都不是.它只是一个声明.如果想更具体一点,可以将其称为非定义声明或不是定义的声明."

Neither the term "forward declaration" nor "prototype declaration" are defined in the C++ standard, so very strictly speaking, it's neither. It is simply a declaration. If you want to be more specific, you could call it a non-defining declaration, or "a declaration which is not a definition."

在标准中使用向前声明"一词时,它们用于指代声明但未定义其声明的事物(函数或类)的声明.按照这种用法, int getnum()因此是一个前向声明.

When the words "forward declaration" are used in the standard, they are used to refer to declarations which declare, but do not define, the thing (function or class) they declare. Following this usage, int getnum() is therefore a forward declaration.

函数的原型声明"在标准(1)中使用得更少,并且大多数时候是在谈论与C的[in]兼容性.但是,当使用它时,它指的是精确的与该函数的前向声明相同的概念.这样,您还可以调用 int getnum(); 原型声明.

"Prototype declaration" of a function is used even less in the standard(1), and mostly when talking about [in]compatibility with C. However, when used, it refers to the exact same concept as a forward declaration of that function. Going by this, you could also call int getnum(); a prototype declaration.

总而言之,术语前向声明"和原型声明"没有正式定义,但是从通常使用和理解的方式来看, int getnum(); 可以描述为

To summarise, the terms "forward declaration" and "prototype declaration" have no formal definition, but from the way they are normally used and understood, int getnum(); can be described as either.

(1)但是,在标准(C ++ 17 N4659 [basic.scope.proto] 6.3中定义了类似的术语函数原型范围" .4/1).它在未定义的函数声明中建立函数参数名称的范围,并且是最接近的封闭函数声明符.

(1) However, a similar term "function prototype scope" is defined in the standard (C++17 N4659 [basic.scope.proto] 6.3.4/1). It establishes the scope of function parameter names in a non-defining function declaration, and it's the closest enclosing function declarator.

这篇关于原型声明和正向声明之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆