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

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

    It might depend on the language, as has been said. I think it really depends on whether the words are used for things like classes. For most of the data types discussed here, the question might not have much relevance. In C++ (see c++ - What is the difference between a definition and a declaration?), a class or struct always has precisely one definition but can be declared zero or more times. A class cannot be declared without a definition. So "declared" might be synonymous with "used".

    In most languages, simple types such as integers do not need definitions in the manner that classes do.

    0 讨论(0)
  • 2020-11-30 09:31

    It is important to use the correct terminology, otherwise people will not know what you are talking about, or incorrectly assume that you don't know what you are talking about.

    0 讨论(0)
  • 2020-11-30 09:36

    The correct answer depends on which language you're talking about. Computer languages often have specific terminology, either because of the language specification or the community grown up around the language. COBOL, back when I used it, had a much different terminology than more mainstream languages (in the sense of languages closer to the mainstream of language development, not mainstream business). Forth developed some strange terminology.

    If you know English, you can usually get a good idea as to what a word means from its normal meaning, but never count on it too much. The same is true with specific words across languages or language communities.

    0 讨论(0)
  • 2020-11-30 09:40

    These terms often have precise meanings in the standards for various languages. When that is the case they should not be conflated.

    In c for instance:

    • a function may be defined only once (when you say what it does), but it may also be declared before that (when you say what arguments it takes and what type it returns).

    • likewise a variable is declared when you say what type it is, and this happens only once for each scope. But you may assign a value repeatedly. (Some languages also differentiate between initialization (giving a variable a value at declaration time) and assignment (changing the value later).)

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题