C++声明和定义的区别
来源:互联网 发布:15套java架构师百度云 编辑:程序博客网 时间:2024/06/05 22:51
C++ primer 58页上说头文件一般包含类的定义,extern变量的声明和函数的声明。
baidu了下关于声明和定义的文章,让人看了后还是很疑惑,还是 MSDN上的一段说明比较清晰:
Declarations and Definitions (C++)
Declarations tell the compiler that a program element or name exists. Definitions specify what code or data the name describes. A name must be declared before it can be used.
A declaration introduces one or more names into a program. Declarations can occur more than once in a program. Therefore, classes, structures, enumerated types, and other user-defined types can be declared for each compilation unit. The constraint on this multiple declaration is that all declarations must be identical. Declarations also serve as definitions, except when the declaration:
Is a function prototype (a function declaration with no function body).
Contains the extern specifier but no initializer (objects and variables) or function body (functions). This signifies that the definition is not necessarily in the current translation unit and gives the name external linkage.
Is of a static data member inside a class declaration.
Because static class data members are discrete variables shared by all objects of the class, they must be defined and initialized outside the class declaration. (For more information about classes and class members, seeClasses.)
Is a class name declaration with no following definition, such as class T;.
Is a typedef statement.
Examples of declarations that are also definitions are:
// Declare and define int variables i and j.int i;int j = 10;// Declare enumeration suits.enum suits { Spades = 1, Clubs, Hearts, Diamonds };// Declare class CheckBox.class CheckBox : public Control{public: Boolean IsChecked(); virtual int ChangeState() = 0;};
Some declarations that are not definitions are:
extern int i;char *strchr( const char *Str, const char Target );
A definition is a unique specification of an object or variable, function, class, or enumerator. Because definitions must be unique, a program can contain only one definition for a given program element. There can be a many-to-one correspondence between declarations and definitions. There are two cases in which a program element can be declared and not defined:
A function is declared but never referenced with a function call or with an expression that takes the function's address.
A class is used only in a way that does not require its definition be known. However, the class must be declared. The following code illustrates such a case:
// definitions.cppclass WindowCounter; // Forward reference; no definitionclass Window{ // Definition of WindowCounter not required static WindowCounter windowCounter;};int main(){}
- 声明和定义的区别(C++)
- C语言定义和声明的区别
- 定义和声明的区别
- 声明和定义的区别
- 声明和定义的区别
- 声明和定义的区别
- 声明和定义的区别
- 定义和声明的区别
- 声明和定义的区别
- 定义和声明的区别
- 声明和定义的区别
- 声明和定义的区别
- 声明和定义的区别
- 定义和声明的区别
- 定义和声明的区别
- 声明和定义的区别
- 声明和定义的区别
- 定义和声明的区别
- 给定一个整数,打印出它的所有素数因子--两种方法,两种效率。
- 企业级JavaEE开发框架bbossgroups作者尹标平专访
- busybox登陆后没要求输入密码的解决办法
- 关于命令行提示符显示格式
- 解压zip压缩包(JAVA)
- C++声明和定义的区别
- 黑马程序员________Java中GUI编程学习笔记
- 在Android中使用VideoView播放视频
- 细说WCF中的会话模式
- 毛泽东邀请西哈努克留居北京内幕-西哈努克-毛泽东-柬埔寨
- XML简介之Schema之XSD 复合空元素 VS XSD 复合类型 - 仅含元素
- Dex文件格式
- Oracle高级查询
- 美国有多少辆汽车?