Difference between prototype declaration and forward declaration?

后端 未结 4 879
暗喜
暗喜 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:09

    Forward declaration is a type of declaration where you specify an Identifier for a Variable, Constant, Type or a Function without giving it's implementation. it actually tells the compiler about an entity with some meta data like name, size etc.

    On the other hand, by prototype declaration for a Function means the declaration of a Function with a name and type signature without specifying the function body. So it's only for the function concept, not for variables, constants or types. And so forward declaration can be regarded as a superset of prototype declaration.

    For the above example, according to definitions, it's both forward declaration and prototype declaration. Hopefully I am not wrong.

提交回复
热议问题