Difference between prototype declaration and forward declaration?

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

    A forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

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

    Hope the following clear things out for you!

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

提交回复
热议问题