×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT技术讨论 / Jabber's Q(3)
    I have another stupid question. Suppose I have a variable defined at the very beginning of a .cpp file. If I put a modifer
    "static" before its declarion, will its scope be wider or narrower?
    • it's a C language concept than C++
      I think it will be narrower.
      when a variable is defined outside any functioin, it is stored as static, no matter whether put "static" keyword before the variable.
      If you put "static" before the variable, that means this variable can only be used within this .cpp file, cannot be used in any other .cpp file which even use "extern" keyword.

      I guess the confusion is we usually use "static" within a function, in this case the "static" change the storage way of a variable from dynamic to static. this "static" is totally different concept as that "static" you mentioned.
    • The correct answer is ...
      The correct answer is that the scope of that variable do not change.
      Its scope remains file scope. Zack's answer confused concept of "scope" with concept of "linkage", and concept of "local duration" with concept of "dynamic duration" in C++ language.
      • Probably both Zack and you are right
        Human languges have great ambiguities. For the same words, people may have different understandings.

        Indeed, the global variables are still available in the file in which they are defined, after a "static" key word is applied.
        However, they are no longer available in other C++ source files even we claim they are "extern".

        Such things sounds trivial to C++ veterans, but not all C++ gurus can explain them elegantly. Thank you for discussions.
        • Thank you for your commnets. To discuss more precisely ...
          Thank you for your commnets.
          To discuss more precisely.
          The scope of a global variable will not change after a "static" key word is applied. But its linkage changes from external to internal after a "static" key word is applied. Scope and linkage are closely related but different concepts in C++.
          I think that a programmer should make efforts to pay attention to details and think precisely.
          • Philosophically, I second to you.
            In the real-time life, people always have different opinions in some disputable things. This is in particular the case in the world of methodologists. In such a case, we should raise our thumb if somebody's opinion is self-contained and he/she can defend for him/herself.

            As far as the question I raised here, I
            think your explanation is self-consistent.
            I am intending to accept your explanation.
            However, I don't think all the C++ programmers will agree with you. As I have said, human lanaguges have intrinsic ambiguities. By the same words, people may mena different things. This is even true among those C++ gurus such as Bruce Eckel and Stanley Lippman.

            For us, it is really diffiuclt to be judges because of our weakpoints in using English.
            • It is sure, as you said, that human lanaguges have intrinsic ambiguities.
              It is sure, as you said, that human lanaguges have intrinsic ambiguities.
              However, I am sorry to say, I don't think that is my own explanation. I am not capable of explaining such thing myself. I think it probably is a specification of C/C++.
              I just try to make things clear.
              I wrote in English because these discussions did not matter very much and provided opportunities for me to practise my terrible English.
              Thank you.