Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. static class variable without definition question

static class variable without definition question

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 1.7k Views
  • 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.
  • J Offline
    J Offline
    JonB
    wrote on 6 Oct 2022, 13:26 last edited by JonB 10 Jun 2022, 13:29
    #1

    I do not follow why I do not get a compiler/linker error message for the following:

    // `.h` file
    class Foo
    {
        static int bar;
    }
    
    // .cpp file
    Foo something;
    

    This compiles and links fine (gcc 9.4.). I expected it to complain "no definition for Foo::bar", or similar.

    If in the .cpp I go e.g. abc = Foo::bar then I do get the error (like Undefined reference to Foo::bar). And then I need to put Foo::bar = 0; into .cpp, as I would expect to have to do.

    My question is: why does C++ allow me to declare the static bar in the class declaration and then fail to provide a definition, so long as I do not try to reference it?

    For example, if I declared a member method, int method();, in the class .h file, it would demand I provided the body for it, that would not be optional if I didn't call the method.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 7 Oct 2022, 10:20 last edited by
      #4

      Class statics are just glorified namespaces. Static member variables are just globals. They don't contribute to class size or layout. Static member functions are, similarly, just a regular global functions. They can't be virtualized and they are not part of vtable. It's like a forward declaration if you don't define it. No harm in it and linker is happy if nothing references it.
      Worth mentioning is that this is the case for simple int. If the member was a class with custom constructor/destructor it couldn't be omitted, because creation and destruction of the static could have side effects.

      J 1 Reply Last reply 7 Oct 2022, 13:36
      5
      • M Offline
        M Offline
        mpergand
        wrote on 6 Oct 2022, 14:53 last edited by mpergand 10 Jun 2022, 14:54
        #2

        Seems the linker doesn't complain until you use it somewhere.
        Foo::bar=0;
        will produce an undefined symbol error

        J 1 Reply Last reply 6 Oct 2022, 15:00
        0
        • M mpergand
          6 Oct 2022, 14:53

          Seems the linker doesn't complain until you use it somewhere.
          Foo::bar=0;
          will produce an undefined symbol error

          J Offline
          J Offline
          JonB
          wrote on 6 Oct 2022, 15:00 last edited by JonB 10 Jun 2022, 15:04
          #3

          @mpergand said in static class variable without definition question:

          Seems the linker doesn't complain until you use it somewhere.

          Yeah. But true for, say, member methods, i.e. error if not defined without you needing to call. Doubtless because of vtable.

          I didn't try what happens with same cases if I tried a static method in class, static int bar(); instead of variable bar. I wonder whether that does not error if you never call Foo::bar()? I suppose that will be the same and will be fine, so static definitions (variables or methods) only matter if you actually use them.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 7 Oct 2022, 10:20 last edited by
            #4

            Class statics are just glorified namespaces. Static member variables are just globals. They don't contribute to class size or layout. Static member functions are, similarly, just a regular global functions. They can't be virtualized and they are not part of vtable. It's like a forward declaration if you don't define it. No harm in it and linker is happy if nothing references it.
            Worth mentioning is that this is the case for simple int. If the member was a class with custom constructor/destructor it couldn't be omitted, because creation and destruction of the static could have side effects.

            J 1 Reply Last reply 7 Oct 2022, 13:36
            5
            • C Chris Kawa
              7 Oct 2022, 10:20

              Class statics are just glorified namespaces. Static member variables are just globals. They don't contribute to class size or layout. Static member functions are, similarly, just a regular global functions. They can't be virtualized and they are not part of vtable. It's like a forward declaration if you don't define it. No harm in it and linker is happy if nothing references it.
              Worth mentioning is that this is the case for simple int. If the member was a class with custom constructor/destructor it couldn't be omitted, because creation and destruction of the static could have side effects.

              J Offline
              J Offline
              JonB
              wrote on 7 Oct 2022, 13:36 last edited by
              #5

              @Chris-Kawa
              Yes, thank you for clarifying, this is about the conclusion I had come to myself.

              I now get that class Foo { static int bar; } behaves for my question's purpose similarly to extern int bar; as a global in C. This implies some module will provide a int bar; implementation/storage definition. However, if nothing in any code actually references that bar then the linker won't care or complain; once something does then the linker will error with "can't find it". In this sense, C++ class-static variables (and for that matter functions/methods) behave the same. So it's just a glorified extern, and I get it now :)

              1 Reply Last reply
              1

              1/5

              6 Oct 2022, 13:26

              • Login

              • Login or register to search.
              1 out of 5
              • First post
                1/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved