In this example i'm trying to return x
, y
or z
from get_by_ptr
function, but can't unedrastand rigth(legal) syntax to do it. I understand that this
pointer holds base address of object and pointer to member
holds "address shift" and I can calculate address. But is there legal syntax way?
class foo {
public:
size_t x{ 33 };
size_t y{ 77 };
size_t z{ 99 };
size_t get_by_ptr(size_t foo::*ptr) {
//how to return x, y or z using ptr
}
};
int main() {
foo bar;
size_t foo::*ptr{ &foo::y };
size_t z{ bar.get_by_ptr(ptr) };
}
Thank You.
CodePudding user response:
You likely want to write:
size_t get_by_ptr(size_t foo::*ptr) {
return this->*ptr;
}