Definition and Declaration

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.

posted @ 2016-06-03 19:51  steven_xiu  阅读(192)  评论(0)    收藏  举报