声明和定义的区别

来源:互联网 发布:手机掌控汽车软件 编辑:程序博客网 时间:2024/05/03 10:54
c++ primer 3rd 5.2声明语句讲得不清不楚的,还是Effective c++ 2nd讲得清楚.所谓声明( declaration),就是用来将一个object function class 或tempate的型别名称告诉编译器.声明式并不带有细节信息.

对object而言,其定义式是编译器为它配置内存的地点.
extern int x; // object declaration
int x; // object definition.

对于function或function template而言,其定义式是提供函数本体( funtion body).
int fun( int arg) // function declaration
int fun( int arg){ return 0;} // function declaration definition.

对于class或class template而言,其定义式必须列出该class或tempalte的所有members;
class Clock; // class declaration.

// class definition
class Clock{
 public:
   Clock();
   ~Clock();
}


原创粉丝点击