码迷,mamicode.com
首页 > 其他好文 > 详细

Definition and Declaration

时间:2016-06-03 21:12:17      阅读:142      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!