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. A question about getting the address of a class static const.
Qt 6.11 is out! See what's new in the release blog

A question about getting the address of a class static const.

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    PikaCat
    wrote on last edited by PikaCat
    #1

    #include <iostream>
    using namespace std;
    class A{
    public:
    static const int a=3;
    };

    int main(){
    cout<<&A::a;
    return 0;
    }
    When I compile the code above. It pops out an error saying: undefined reference to A::a. But I have already initialized a to be 3, So could you please explain it to me?

    JonBJ 1 Reply Last reply
    0
    • P PikaCat

      #include <iostream>
      using namespace std;
      class A{
      public:
      static const int a=3;
      };

      int main(){
      cout<<&A::a;
      return 0;
      }
      When I compile the code above. It pops out an error saying: undefined reference to A::a. But I have already initialized a to be 3, So could you please explain it to me?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @PikaCat
      https://stackoverflow.com/questions/18107077/how-to-access-the-address-of-a-static-const-member-of-a-class

      You're going to need to add const int A::a in the .cpp, to give it storage. And note that your code does compile, it does not link. Plus, apparently, you're likely using gcc because (some?) MSVC does allow what you have written?

      1 Reply Last reply
      1
      • P Offline
        P Offline
        PikaCat
        wrote on last edited by
        #3

        Thanks:)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Konstantin Tokarev
          wrote on last edited by
          #4

          You can also use constexpr instead of const in your example to make it working correctly.

          JonBJ 1 Reply Last reply
          1
          • K Konstantin Tokarev

            You can also use constexpr instead of const in your example to make it working correctly.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Konstantin-Tokarev
            You are right, but I am surprised, I thought constexpr gave you more of a compile-time constant. (I am not a C++ expert.)

            1 Reply Last reply
            0

            • Login

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