定义和声明有什么区别? [英] What is the difference between a definition and a declaration?

查看:34
本文介绍了定义和声明有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者的含义都让我难以理解.

解决方案

声明 引入标识符并描述其类型,无论是类型、对象还是函数.声明是编译器需要来接受对该标识符的引用.这些是声明:

extern int bar;extern int g(int, int);双 f(int, double);//函数声明可以省略 extern类 foo;//类型声明不允许使用 extern

定义 实际上实例化/实现了这个标识符.链接器需要将引用链接到这些实体.这些是对应于上述声明的定义:

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

定义可以用来代替声明.

标识符可以根据需要声明.因此,以下在 C 和 C++ 中是合法的:

double f(int, double);双 f(int, double);extern double f(int, double);//和上面两个一样extern double f(int, double);

但是,它必须定义恰好一次.如果您忘记定义已在某处声明和引用的内容,则链接器不知道将引用链接到什么内容并抱怨缺少符号.如果您多次定义某些内容,则链接器不知道将引用链接到哪些定义,并会抱怨重复的符号.

<小时>

由于 C++ 中什么是类声明与类定义的争论不断出现(在其他问题的答案和评论中),我将粘贴一个此处引用 C++ 标准.
在 3.1/2,C++03 说:

<块引用>

声明就是定义,除非它[...] 是类名声明[...].

3.1/3 然后举几个例子.其中:

<前>[例子: [...]struct S { int a;国际b;};//定义 S、S::a 和 S::b [...]结构体;//声明 S——结束例子

总结一下:C++标准认为struct x;是一个声明,而struct x {};是一个定义.(换句话说,前向声明"用词不当,因为在 C++ 中没有其他形式的类声明.)

感谢 litb (Johannes Schaub) 在他的一个答案.

The meaning of both eludes me.

解决方案

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.

这篇关于定义和声明有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆