简体   繁体   中英

C++ typedef with strange scope resolution operator

I have to port some C++ code and found this strange typedef:

typedef uint32_t SomeClassName::* ptr;

The target compiler is MSVC++. As far as I understand, this just creates an alias for the uint32_t* named ptr . The part with SomeClassName:: does nothing useful and should be treated as an error by the compliant C++ compiler. Am I right or wrong?

Also, found Unusual scope resolution operator question on SO, that possibly answers this question, but I'm not sure about it.

This is a pointer to member. Specifically a variable of type ptr can point to any uint32_t data member of SomeClassName . It can be used like this:

struct Foo {
    int a;
    int b;
    float c;
};

Foo foo;
int Foo::* ptr;

ptr = &Foo::a;
foo.*ptr = 10; //Set foo.a to 10

ptr = &Foo::b;
foo.*ptr = 15; //Set foo.b to 15

//ptr = &Foo::c; //Won't compile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM