1

I have a header file which defines two objects:

// header.h
static A object1("param1", "param2");
static A object2("param1", "param2");

This is how A looks like:

class A {
    public:
    int random;
    A(char* p1, char* p2){
        printf("Called constructor for %s | %s\n", p1, p2);
    }
};

However, the constructor is never getting called. I'm doing this:

#include "header.h"

int main(){
    // to prevent optimization issues
    object1.random = rand();
    if (object1.random != 3) { printf("\n"); }
    return 0;
}

And I never see the messages on the console, it just remains empty.

I've read about the static initialization order fiasco, but I believe it's not the problem here, because neither of those objects is relying on the other one, so order doesn't really matter...

I'm using MSVC++ 2013 on Windows 7 x64

rev
  • 1,861
  • 17
  • 27
  • 2
    The code is not perfect -- you should use `A(char const* p1, char const* p2) {` -- but it should work. – R Sahu Apr 06 '15 at 04:12
  • Which compiler/version are you using, and with what compile flags? – rici Apr 06 '15 at 04:29
  • 1
    It may have something to do with optimization. Try referencing `object1` and `object2` in `main`. – Lingxi Apr 06 '15 at 04:29
  • Can you add a little more details about your environment? Compiler, version, OS etc. – Mohit Jain Apr 06 '15 at 04:45
  • Please use breakpoint to watch console, if you use Win7 and Visual Studio, it should be OK. – thinkerou Apr 06 '15 at 04:54
  • @rici and MohitJain: Windows 7 x64, Visual Studio 2013. Lingxi: tried, doesn't change anything. thinkerou I did add a breakpoint, but it doesn't ever hit. – rev Apr 06 '15 at 10:57
  • Are they inside a class or just floating around? In the latter case, you will have a copy of them for every source file. – Neil Kirk Apr 06 '15 at 11:05
  • Do you see the blank line from `printf("\n");` being printed? – Mike DeSimone Apr 06 '15 at 11:11
  • @MikeDeSimone yes, I do. NeilKirk: they're just 'floating around', yes. Does that change the fact that they need to be constructed before used? – rev Apr 06 '15 at 21:22

1 Answers1

0

When are static C++ class members initialized? contains a great answer about this topic.

In short there are 3 guarantees (citing the original Tadeusz Kopec answer in the above mentioned question):

  1. Objects defined in the same translation unit (usually it means .cpp file) are initialized in order of their definitions
  2. Initialization of static objects from a translation unit will be done before use of any object or function from this translation unit
  3. They will be initialized before main is entered.

If in your specific case static variables object1 and object2 were not initialized this could be related to a compiler optimization omitting the initialization of unused variables. You can verify this easily by adding a usage of these variables inside main and observe whether these got initialized and when.

Community
  • 1
  • 1
Amnon Shochot
  • 8,998
  • 4
  • 24
  • 30
  • 1
    check the edit -- I added code to avoid optimizations (and I'm compiling in `Debug` mode with disabled optimizations), and still it doesn't get called. – rev Apr 06 '15 at 11:00
  • 1
    Besides, these aren't static class members, they're static objects, which means they lack external linkage. – Mike DeSimone Apr 06 '15 at 11:06