gpt4 book ai didi

C++ 指向成员成员的指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:47 25 4
gpt4 key购买 nike

//#define NOT_WORKS
#define HOW(X) 0

struct A {

};

struct B {
A a;
};

struct C {
B b;
};

int main(int argc, char **argv) {
A B::*ba = &B::a; // ba is a pointer to B::a member
B C::*cb = &C::b; // cb is a pointer to C::b member

#ifdef NOT_WORKS

{ A C::*ca = &C::b::a; } // error: Symbol a could not be resolved / error: ‘C::b’ is not a class or namespace
{ A C::*ca = cb + ba; } // error: invalid operands of types ‘B C::*’ and ‘A B::*’ to binary ‘operator+’

A C::*ca = HOW(???); // is possible to reach C::b::a by a member pointer?

#endif

C cptr;
A aptr = cptr.*cb.*ba; // is pointer inference chaining the only solution?

return 0;
}

如果成员指针的推理链是到达内部成员的唯一解决方案,我可以使用模板将其封装在单一类型中吗?


现在可以用gcc编译代码了

谢谢大家

最佳答案

is possible to reach C::b::a by a member pointer?

类型:

C c; 
A B::*ca = &B::a; // usage: c.b.*ca;

is pointer inference chaining the only solution?

关于C++ 指向成员成员的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16438400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com