extern sample in MSDN

This is a discussion on extern sample in MSDN within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I think in practical experience, extern is useful only when we have more than one compile unit (cpp ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    extern sample in MSDN

    Hello everyone,


    I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units.

    In the MSDN extern sample,

    http://msdn2.microsoft.com/en-us/library/0603949d.aspx

    It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the file is then compiled to a static library, yes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Your reply yes,

    1. is it to answer the code (using extern to qualify a variable, which is define in the same compile unit) is correct?

    2. Or, it is of practical usage to use extern to qualify a variable, which is define in the same compile unit.

    Quote Originally Posted by CornedBee View Post
    If the file is then compiled to a static library, yes.

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's correct. It's even practical if it's in the same compilation unit, because included headers are part of the compilation unit. It's not really practical if it's in the same source file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    "It's even practical if it's in the same compilation unit, because included headers are part of the compilation unit"

    I think you mean the header file contains extern declaration and it is included in the current source file, and in the current source file there is definition of the variable, right?

    In the above situation I described, what is the practical advantages?

    Quote Originally Posted by CornedBee View Post
    It's correct. It's even practical if it's in the same compilation unit, because included headers are part of the compilation unit. It's not really practical if it's in the same source file.

    regards,
    George

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In the above situation I described, what is the practical advantages?
    It takes up less space in the MSDN database.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Interesting, CornedBee. :-)


    Any comments to my post #5?

    Quote Originally Posted by CornedBee View Post
    It takes up less space in the MSDN database.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSDN template sample
    By George2 in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2008, 04:47 AM
  2. MSDN volatile sample
    By George2 in forum C++ Programming
    Replies: 38
    Last Post: 01-05-2008, 06:59 AM
  3. MSDN for_each sample
    By George2 in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2007, 04:12 AM
  4. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 08:30 AM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21