CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Does a static class var work in a static lib ?

    Consider the following code. Obviously the class is intended to get exported from a DLL - so there'll only ever be 1 x copy of some_var:-

    Code:
    class __declspec(dllexport) whatever
     {
      public:
    	static int some_var;
    };
    But I've occasionally seen this in code that was intended for a static lib - obviously without __declspec(dllexport) . The lib might be needed by multiple DLL's - so wouldn't they each have their own copy of some_var? And wouldn't that defeat the point of declaring as static? Or does this kinda thing get sorted out at runtime somehow?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,561

    Re: Does a static class var work in a static lib ?

    A static lib is linked into the exec code at compile time.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.1)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: Does a static class var work in a static lib ?

    Thanks 2kaud - I can see how that'd work if the exe only ever uses static libs. But if the exe links to 3 x DLL's (and it's the DLL's which each need the static lib) surely there'll be 3 x copies of the class's static var?
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,561

    Re: Does a static class var work in a static lib ?

    It's been too long since I last used dynamic .dlls. We don't use them any more as we consider there's too many problems with them...

    Why not create a simple test program and find out? That's what I do when something isn't working as I think it should or I'm not understanding something or just want to try things out or mess about with a new feature or...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.1)

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