所以我有这个代码:
class xx
{
int getnum(); //Is this a forward declaration or a prototype declaration and why?
};
int xx::getnum()
{
return 1+3;
}
所以这个问题已经在代码中注释了,但是:
Is int getnum();
前向声明或原型声明,为什么?
C++ 标准中都没有定义术语“前向声明”和“原型声明”,所以严格来说,两者都不是。这简直就是一个宣言。如果你想更具体,你可以称之为非定义性声明,或“不是定义的声明”。
当标准中使用“前向声明”一词时,它们用于指声明但不定义它们声明的事物(函数或类)的声明。按照这种用法,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();
可以描述为任一。
(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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)