zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    uva129
    uva167
    【Linux】touch命令
    【Linux】od命令
    【Linux】tail命令
    【Linux】head命令
    【Linux】less命令
    【Linux】more命令
    【Linux】nl命令
    【Linux】cat命令
  • 原文地址:https://www.cnblogs.com/stevenxiu/p/5557440.html
Copyright © 2011-2022 走看看