Difference between prototype declaration and forward declaration?

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;
}

      

So the question is already commented out in the code, but:

Is it int getnum();

prototype or prototype declaration and why?

+3


source to share


4 answers


Neither the term forward declaration nor prototype declaration is defined in the C ++ standard, so strictly speaking it is not. It's just a declaration. If you want to be more specific, you can call this a non-defining declaration or "a declaration that is not a definition".

When the words "forward declaration" are used in the standard, they are used to refer to declarations that declare, but do not define, the subject (function or class) they declare. After this use int getnum()

is thus a forward declaration.

The "prototype declaration" of a function is used even less in the standard (1) and mostly when it comes to C compatibility. However, when used, it refers to the exact same concept as the front declaration of that function. Following this, you can also invoke int getnum();

a prototype declaration.



To summarize, the terms "forward declaration" and "prototype declaration" do not have a formal definition, but from the way they are commonly used and understood, they int getnum();

can be described as.


(1) However, a similar term "function prototype scope" is defined in the standard (C ++ 17 N4659 [basic.scope.proto] 6.3.4 / 1). It sets the scope of the function parameters in a non-defining function declaration and is the closest function declaration.

+6


source


C ++ only allows full function prototype declarations, unlike C, in which something like int getnum();

could be a direct declaration of something likeint getnum(int);



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

8.3.5 Edit: 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 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).

Impact on original function : changing the semantics of a well-defined function. This feature has been deprecated in C.

Complexity of transformation : syntactic transformation. Function declarations that use the C incomplete declaration style must be filled in to become full prototype declarations. It may be necessary to further modernize the program if different calls of the same (non-prototype) function have a different number of arguments or if the type of the corresponding arguments was different.

+1


source


The first declaration is an identifier declaration (denoting an object such as a type, variable, constant, or function) for which the programmer has not yet fully defined.

On the other hand, prototype refers to a function, not an identifier.

Look forward to the following clear things!

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" ;
}

      

0


source


A Forward declaration is a type of declaration in which you provide an Identifier for a variable, constant, type, or function without providing an implementation. it actually tells the compiler about an object with some metadata like name, size, etc.

On the other hand, declaring a prototype for a function means declaring a function with a name and signature type without specifying a function body. So this is only for the concept of a function, not for variables, constants or types. And so a declaration in its direct expression can be seen as a superset of the prototype declaration.

In the above example, according to the definitions, it is both a declaration of a declaration and a prototype. I hope I am not wrong.

0


source







All Articles