Search code examples
c++mysqlscoperesolution

What means scope resolution of structure in a variable declaration?


I found this code in MySQL source in file set_var.h and I'm not sure what does ulong SV::*offset mean.

In short it looks like:

struct SV {...}

class A {

    ulong SV::*offset;

    A(ulong SV::*offset_arg): offset(offset_arg) {...}
};

class B {

    DATE_TIME_FORMAT *SV::*offset;

    B(DATE_TIME_FORMAT *SV::*offset_arg) : offset(offset_arg) {...}
}

and so on.


Solution

  • ulong SV::*offset; is a member of the class A named offset which points to a member of the class SV of type ulong. It's used like this :

    #include <iostream>
    
    using ulong = unsigned long;
    struct SV {
        ulong x, y, z;
    };
    
    int main()
    {
        // A pointer to a ulong member of SV
        ulong SV::*foo;
    
        // Assign that pointer to y
        foo = &SV::y;
    
        // Make an instance of SV to test
        SV bar;
        bar.x = 10;
        bar.y = 20;
        bar.z = 30;
    
        // Dereference with an instance of SV
        // Returns "20" in this case
        std::cout << bar.*foo;
    
        return 0;
    }