Difference between prototype declaration and forward declaration?

后端 未结 4 918
暗喜
暗喜 2021-01-14 08:49

So I have this code:

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

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


        
4条回答
  •  感动是毒
    2021-01-14 09:14

    C++ only allows full prototype declarations of functions, unlike C in which something like int getnum(); could be a forward declaration of something like 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.

提交回复
热议问题