简体   繁体   English

具有奇怪的作用域解析运算符的C ++ typedef

[英]C++ typedef with strange scope resolution operator

I have to port some C++ code and found this strange typedef: 我必须移植一些C ++代码并发现以下奇怪的typedef:

typedef uint32_t SomeClassName::* ptr;

The target compiler is MSVC++. 目标编译器是MSVC ++。 As far as I understand, this just creates an alias for the uint32_t* named ptr . 据我了解,这只是为uint32_t*创建了一个名为ptr的别名。 The part with SomeClassName:: does nothing useful and should be treated as an error by the compliant C++ compiler. 具有SomeClassName::的部分没有任何用处,并且应由兼容的C ++编译器视为错误。 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. 另外,在SO上发现了异常范围解析运算符问题,它可能回答了这个问题,但是我不确定。

This is a pointer to member. 这是指向成员的指针。 Specifically a variable of type ptr can point to any uint32_t data member of SomeClassName . 特别是ptr类型的变量可以指向SomeClassName任何uint32_t数据成员。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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