What exactly are C++ definitions, declarations and

2019-01-09 06:36发布

问题:

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 the first time I assign to a variable? Or is there more to it than that?

回答1:

Define and declare are similar but assign is very different.

Here I am declaring (or defining) a variable:

int x;

Here I am assigning a value to that variable:

x = 0;

Here I am doing both in one statement:

int x = 0;

Note

Not all languages support declaration and assignment in one statement:

T-SQL

declare x int;
set x = 0;

Some languages require that you assign a value to a variable upon declaration. This requirement allows the compiler or interpreter of the language to infer a type for the variable:

Python

x = 0


回答2:

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();


回答3:

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.



回答4:

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).)



回答5:

The differences can seem subtle, but they are important. Not every language makes the same distinctions, but in C++ a variable declaration makes the type and name of the variable known to the compiler

int i;

A variable definition allocates storage and specifies an initial value for the variable.

i = 1;

You can combine a variable declaration and definition into one statement, as is commonly done.

int x = 1;

Declaring a variable inside a function will also set aside memory for the variable, so the following code implicitly defines variable a as a part of its declaration.

int main()
{
    int a;
    return 0;
}

Since variable a is automatically defined by the compiler, it will contain whatever value was in the memory location that was allocated for it. This is why it is not safe to use automatic variables until you've explicitly assigned a known value to them.

An assignment takes place any time you change the value of a variable in your program.

x = 2;
x++;
x += 4;

A function declaration, similar to the variable declaration, makes the function signature known to the compiler. This allows you to call a function in your source code before it is defined without causing a compiler error.

int doSomething(float x);

A function definition specifies the return type, name, parameter list, and instructions for a function. The first three of these elements must match the function declaration. A function must only be defined once in a given program.

int doSomething(float x)
{
    if( x < 0 )
    {
        x = -x;
    }
    return static_cast<int>(x);
}

You can combine the function decalartion and definition into one, but you must do so before the function is called anywhere in your program.



回答6:

Definition, declaration, and assignment have two cases:

  1. for Variables.
  2. for Functions.

For Variable:

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

int x;

-- Deceleration:
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);


回答7:

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.



回答8:

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.