What is the difference between a definition and a

2018-12-30 23:00发布

The meaning of both eludes me.

22条回答
回忆,回不去的记忆
2楼-- · 2018-12-30 23:01

To understand the nouns, let's focus on the verbs first.

declare - to announce officially; proclaim

define - to show or describe (someone or something) clearly and completely

So, when you declare something, you just tell what it is.

// declaration
int sum(int, int);

This line declares a C function called sum that takes two arguments of type int and returns an int. However, you can't use it yet.

When you provide how it actually works, that's the definition of it.

// definition
int sum(int x, int y)
{
    return x + y;
}
查看更多
不流泪的眼
3楼-- · 2018-12-30 23:02

A declaration presents a symbol name to the compiler. A definition is a declaration that allocates space for the symbol.

int f(int x); // function declaration (I know f exists)

int f(int x) { return 2*x; } // declaration and definition
查看更多
与风俱净
4楼-- · 2018-12-30 23:03

Declaration means give name and type to a variable (in case of variable declaration) eg:

 int i;  

or give name,return type and parameter(s) type to a function without body(in case of function declaration)

eg:

int max(int, int);

whereas definition means assign value to a variable (in case of variable definition). eg:

i = 20;

or provide/add body(functionality) to a function is called function definition.

eg:

 int max(int a, int b)
 {
    if(a>b)   return a;
    return b;  
 }

many time declaration and definition can be done together as:

int i=20;   

and

int max(int a, int b)
{
    if(a>b)   return a;
    return b;    
} 

In above cases we define and declare variable i and function max()

查看更多
低头抚发
5楼-- · 2018-12-30 23:05

A declaration introduces an identifier and describes its type, be it a type, object, or function. A declaration is what the compiler needs to accept references to that identifier. These are declarations:

extern int bar;
extern int g(int, int);
double f(int, double); // extern can be omitted for function declarations
class foo; // no extern allowed for type declarations

A definition actually instantiates/implements this identifier. It's what the linker needs in order to link references to those entities. These are definitions corresponding to the above declarations:

int bar;
int g(int lhs, int rhs) {return lhs*rhs;}
double f(int i, double d) {return i+d;}
class foo {};

A definition can be used in the place of a declaration.

An identifier can be declared as often as you want. Thus, the following is legal in C and C++:

double f(int, double);
double f(int, double);
extern double f(int, double); // the same as the two above
extern double f(int, double);

However, it must be defined exactly once. If you forget to define something that's been declared and referenced somewhere, then the linker doesn't know what to link references to and complains about a missing symbols. If you define something more than once, then the linker doesn't know which of the definitions to link references to and complains about duplicated symbols.


Since the debate what is a class declaration vs. a class definition in C++ keeps coming up (in answers and comments to other questions) , I'll paste a quote from the C++ standard here.
At 3.1/2, C++03 says:

A declaration is a definition unless it [...] is a class name declaration [...].

3.1/3 then gives a few examples. Amongst them:

[Example: [...]
struct S { int a; int b; }; // defines S, S::a, and S::b [...]
struct S; // declares S
—end example

To sum it up: The C++ standard considers struct x; to be a declaration and struct x {}; a definition. (In other words, "forward declaration" a misnomer, since there are no other forms of class declarations in C++.)

Thanks to litb (Johannes Schaub) who dug out the actual chapter and verse in one of his answers.

查看更多
看风景的人
6楼-- · 2018-12-30 23:05

My favorite example is "int Num = 5" here your variable is 1. defined as int 2. declared as Num and 3. instantiated with a value of five. We

  • Define the type of an object, which may be built-in or a class or struct.
  • Declare the name of an object, so anything with a name has been declared which includes Variables, Funtions, etc.

A class or struct allows you to change how objects will be defined when it is later used. For example

  • One may declare a heterogeneous variable or array which are not specifically defined.
  • Using an offset in C++ you may define an object which does not have a declared name.

When we learn programming these two terms are often confused because we often do both at the same time.

查看更多
姐姐魅力值爆表
7楼-- · 2018-12-30 23:06

This is going to sound really cheesy, but it's the best way I've been able to keep the terms straight in my head:

Declaration: Picture Thomas Jefferson giving a speech... "I HEREBY DECLARE THAT THIS FOO EXISTS IN THIS SOURCE CODE!!!"

Definition: picture a dictionary, you are looking up Foo and what it actually means.

查看更多
登录 后发表回答