什么意味着变量声明中结构的范围解析?

时间:2017-01-11 01:55:23

标签: c++ mysql scope resolution

我在文件set_var.h中的MySQL源代码中找到了这段代码,我不确定ulong SV :: * offset的含义是什么。

简而言之,它看起来像:

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) {...}
}

等等。

1 个答案:

答案 0 :(得分:1)

ulong SV::*offset;是名为A的班级offset的成员,该班级指向类型为SV的班级ulong的成员。它是这样用的:

#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;
}