Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. use of extern keyword

use of extern keyword

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swaapu
    wrote on last edited by
    #1

    Hi! I am working on MFC SDI application. can anybody tell me any good example of using extern keyword. can extern be used for passing value of any string variable from one class to another. If it can be used, then how? thanx

    G B J 3 Replies Last reply
    0
    • S swaapu

      Hi! I am working on MFC SDI application. can anybody tell me any good example of using extern keyword. can extern be used for passing value of any string variable from one class to another. If it can be used, then how? thanx

      G Offline
      G Offline
      G Haranadh
      wrote on last edited by
      #2

      **swaapu says:**can extern be used for passing value of any string variable from one class to another? No it is not possiable using extern. extern is for different purpose. if you want to declare later and but if you want to use before itself then use key word extern. Nice talking to you. :-O

      1 Reply Last reply
      0
      • S swaapu

        Hi! I am working on MFC SDI application. can anybody tell me any good example of using extern keyword. can extern be used for passing value of any string variable from one class to another. If it can be used, then how? thanx

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, The extern keyword is used to say to the compiler that you are using a variable declared in another translation unit. See the following for example: You have one translation unit where you define your variable

        // file1.cpp
        int g_nSomeInt = 0;

        And another one where you want to use the variable

        // file2.cpp
        extern int g_nSomeInt;

        void DoFoo()
        {
        g_nSomeInt++;
        }

        This is how extern can be used. It is adviced though too use accessor functions instead of the extern keyword. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        1 Reply Last reply
        0
        • S swaapu

          Hi! I am working on MFC SDI application. can anybody tell me any good example of using extern keyword. can extern be used for passing value of any string variable from one class to another. If it can be used, then how? thanx

          J Offline
          J Offline
          John R Shaw
          wrote on last edited by
          #4

          The extern keyword simply states that the variable or function is declared in a seperate translational unit. That is it is declared and defined in a seperate file from the one that depends on it. Declaring a function prototype as external, only serves to inform the coder that it is declared and defined in another file. Defining a variable as external, informs the complire that it is declared and defined golobaly in another file. The difference between a function protype and a global variable, is that a global variable must be declared external and a prototype is external by default. I've workd with C code that required alot on global variables (dealing with hardware specific addresses). In that case a file was set off to the side to define all the hardware specific data (global variables), which needed to be declared as extern. INTP Every thing is relative...

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups