首页 > 其他 > 详细

Definition and Declaration

时间:2016-06-03 21:09:27      阅读:152      评论:0      收藏:0      [点我收藏+]

1. Variable:

------------

int a ;

------------

This is not only a Declaration, but also a Definition.

Since it is a defintion,  it is must "Define only once".

 

----------------------example------------------

If you write this in a .h file, and included it everywhere...

We know that "#include" means extracted the original file totally here,  so the same var int a has been defined as many times as been included, which is invalid.

ps: compile is ok, since it is local, local in the specific cpp file. When complier is working on this file, he dosen‘t know even a bit information the world outside.

So the generated file .o is also local.

However, Link is not ok. For multi definition error of int a.

 

 

 

 

2.Function:

-------------

void f();

-------------

So you can declare it many times, everywhere. This is just a name. Comile and Link are both ok.

 

So when you define Variable in a .h file,  "static" "extern" "const" ... are needed.

Definition and Declaration

原文:http://www.cnblogs.com/stevenxiu/p/5557440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!