What exactly are C++ definitions, declarations and assignments?

前端 未结 8 1255
长情又很酷
长情又很酷 2020-11-30 08:52

I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for

8条回答
  •  眼角桃花
    2020-11-30 09:45

    General Role: Definition = declaration + reserved space.

    Definition, declaration, and assignment have two cases:

    1. for Variables.
    2. for Functions.

    For Variables:

    -- Definition:
    To tell the compiler to reserve memory for the variable.

    int x;
    

    -- Declaration:
    To tell the compiler that the variable defined in somewhere else.

    extern int x;
    

    -- Assignment:
    To tell the compiler to put the value in the variable.

    x = 0;
    

    For Functions:

    -- Definition:

    int functionDef(int x){
      int x;  
      ...  
      ...  
      ...  
      return x;  
    }
    

    -- Declaration: It is just the prototype of the function.

    int functionDef(int x);
    

提交回复
热议问题