CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2009
    Posts
    3

    Internal linkage in .cpp files: why bother?

    Hi,

    I'm still grappling with the concepts of linkage and memory management. I'm from a Java/C# background, so I'm used to static classes, and I'm trying to make the transition to namespaces where appropriate.

    So I was reading up on the use of "private/helper" functions for namespaces being defined within an anonymous namespace to force internal linkage.

    My question is this: what's the point of forcing internal as opposed to external linkage if these functions are defined in a .cpp file? Clients of the namespace will be including the header file, so anything of global scope declared/defined in the .cpp shouldn't be visible anyhow, right? Why bother with the anonymous namespace trick at all, in this case?
    Last edited by cmc5788; May 1st, 2009 at 01:46 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Internal linkage in .cpp files: why bother?

    Quote Originally Posted by cmc5788
    My question is this: what's the point of forcing internal as opposed to external linkage if these functions are defined in a .cpp file? Clients of the namespace will be including the header file, so anything of global scope declared/defined in the .cpp shouldn't be visible anyhow, right?
    But they could say, define a function with the same name, in which case the external linkage would mean a linker error as the function would appear to have been defined in each translation unit. Alternatively, a smart alec who sees that such a function is defined could declare the function and then use it, and then complain after you remove the function during some refactoring.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2009
    Posts
    3

    Re: Internal linkage in .cpp files: why bother?

    Quote Originally Posted by laserlight View Post
    But they could say, define a function with the same name, in which case the external linkage would mean a linker error as the function would appear to have been defined in each translation unit. Alternatively, a smart alec who sees that such a function is defined could declare the function and then use it, and then complain after you remove the function during some refactoring.
    Ah, I guess I didn't think of it that way. Thanks for the response

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Internal linkage in .cpp files: why bother?

    Summary from laserlight opinion is name crash.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured