原型声明和前向声明有什么区别?

3

所以我有这段代码:

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

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

这个问题在代码中已经进行了评论,但是:

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

4个回答

6
在C++标准中,"forward declaration"和"prototype declaration"这两个术语都没有定义,所以严格来说,它们都不是。它只是一个声明。如果你想更具体地描述它,你可以称之为"non-defining declaration",或者是"不是定义的声明"。
当标准中使用"forward declaration"这个词时,它指的是那些声明了但没有定义函数或类的声明。按照这种用法,int getnum()就是一个forward declaration。
"Prototype declaration"这个词在标准中甚至更少被使用(1),而且主要是在谈论与C的兼容性时使用。然而,在使用时,它指的是与该函数的forward declaration完全相同的概念。因此,你也可以将int getnum()称为prototype declaration。
总之,尽管"forward declaration"和"prototype declaration"这两个术语没有正式的定义,但从它们通常被使用和理解的方式来看,int getnum()可以被描述为任意一个。

1
−1 这个答案是错误的,误导性的,不实之词。 - Cheers and hth. - Alf
1
例如,请参见[input.output.general] - rustyx
@Cheersandhth.-Alf 我觉得这个评价有点苛刻,尽管如此,我已经重写了答案,希望能够改善。在您看来,我成功了吗? - Angew is no longer proud of SO
+1 @Angew:我觉得自己没有资格评估当前答案的好坏。原来的回答是错误的,这个版本似乎是正确的。作为可能的进一步改进,你可以提到“原型范围”(一个术语,在标准中也被正式定义)。无论如何,既然不再是错误的,我将取消踩和点赞。我主要使用投票系统来表明什么是正确和有用的,什么是明显和无可争议的错误。 - Cheers and hth. - Alf
@Cheersandhth.- Alf 说的很有道理,“函数原型作用域”的提及已经添加。 - Angew is no longer proud of SO

1

C++只允许函数进行完整的原型声明,与C不同,例如int getnum();在C中可以是int getnum(int);的前向声明。

C.1.7 Clause 8: declarators [diff.decl]

8.3.5 Change: In C ++ , a function declared with an empty parameter list takes no arguments. In C, an empty parameter list means that the number and type of the function arguments are unknown.

Example:

int f(); // means int f(void) in C ++, int f( unknown ) in C

Rationale: This is to avoid erroneous function calls (i.e., function calls with the wrong number or type of arguments).

Effect on original feature: Change to semantics of well-defined feature. This feature was marked as “obsolescent” in C.

Difficulty of converting: Syntactic transformation. The function declarations using C incomplete declaration style must be completed to become full prototype declarations. A program may need to be updated further if different calls to the same (non-prototype) function have different numbers of arguments or if the type of corresponding arguments differed.


0

前向声明是一种声明类型,其中您为变量、常量、类型或函数指定标识符,而不给出其实现。它实际上告诉编译器有关具有某些元数据(如名称、大小等)的实体。

另一方面,通过原型声明函数意味着声明具有名称和类型签名的函数,而不指定函数体。因此,它仅适用于函数概念,而不适用于变量、常量或类型。因此,前向声明可以被视为原型声明的超集。

对于上述示例,根据定义,它既是前向声明又是原型声明。希望我没有错。


0
前向声明是指程序员尚未给出完整定义的标识符声明(表示实体,如类型、变量、常量或函数)。
另一方面,原型是指函数而非标识符。
希望以下内容能为您澄清问题!
int getnum(); // Function prototype. You have not yet implemented the body of getnum() function, thus its a forward delcaration. 
class RandomClass; // Forward declaration of RandomClass. You have not yet implemented this class but you need it for the rest of your code.

class xx{
    RandomClass *foo; // Our need of having a member like that, made us make a forward declaration of the class RandomClass, above class xx
    void BarFunction(); // Function Prototype!
};

int getnum(){ //This is the simply the body of your prototype above. Has nothing to do with the classes
    return 1+3;
}

void BarFUnction(){
     cout << "foo-bar\n" ;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接