Declarations vs definitions

后端 未结 6 2224
名媛妹妹
名媛妹妹 2021-02-19 18:10

In C# how does a declaration differ from a definition, i.e.:

  1. A class declaration vs a class definition
  2. A variable declaration vs definition
  3. A met
6条回答
  •  时光说笑
    2021-02-19 19:00

    According to Kernighan & Ritchie in "The C Programming Language": A "declaration" announces the properties of variables; it consists of a name and a list of variables, such as: int fahr, celsius;

    According to Stroustrup in "The C++ Programming Language": A "declaration" is a statement that introduces a name into the program. It specifies a type for that name. A type defines the proper use of a name or an expression.

    Neither book specifically defines "definition". But both use the term in the scientific sense of the VALUE of a variable. Thus a function declaration declares the function's calling signature. A function definition contains the actual code.

    The need for having separate meanings in these languages is because of yesteryear's compilers. They needed to know the types of names ahead of time, before the name was actually used. Otherwise they would need to make another pass through the source code.

提交回复
热议问题