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

前端 未结 8 1265
长情又很酷
长情又很酷 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:43

    A definition is where a value or function is described, i.e. the compiler or programmer is told precisely what it is, e.g.

    int foo()
    {
      return 1;
    }
    
    int var; // or, e.g. int var = 5; but this is clearer.
    

    A declaration tells the compiler, or programmer that the function or variable exists. e.g.

    int foo();
    extern int var;
    

    An assignment is when a variable has its value set, usually with the = operator. e.g.

    a = b;
    a = foo();
    

提交回复
热议问题