Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与接线员混淆_C++_Operators - Fatal编程技术网

C++ 与接线员混淆

C++ 与接线员混淆,c++,operators,C++,Operators,当我试图理解下面的代码时,我感到困惑。有谁能解释一下这种黑客行为: a.*b 或者如果a是指向类的指针: a->*b 这两个运算符都用于取消引用指向成员的指针。与常规指针不同,指向成员的指针不能自行取消引用,但必须应用于该类型的实际对象。这些二进制运算符拾取左侧的对象(或指针),并将指针应用于该对象的成员 struct test { int a, b, c; }; int main() { int test::*ptr; ptr = &test::a;

当我试图理解下面的代码时,我感到困惑。有谁能解释一下这种黑客行为:

a.*b
或者如果
a
是指向类的指针:

a->*b

这两个运算符都用于取消引用指向成员的指针。与常规指针不同,指向成员的指针不能自行取消引用,但必须应用于该类型的实际对象。这些二进制运算符拾取左侧的对象(或指针),并将指针应用于该对象的成员

struct test {
    int a, b, c;
};
int main() {
   int test::*ptr;
   ptr = &test::a;
   test t;
   t.*ptr = 5;         // set t.a to 5
   ptr = &test::b;
   test *p = &t;
   p->*ptr = 10;       // set t.b to 10 through a pointer
}

它不是黑客;它是指向成员的指针。