c++ - Some questions on pointer to member of class -



c++ - Some questions on pointer to member of class -

i want inquire behaviour of compiler in next scenario :-

class { public: int n; int *ptrclass; }; int main() { a; int a::*ptr; ptr = &a::n; a.ptrclass = &a.n; cout << "\npointer points memory address: " << ptr; cout << "\npointer points value: " << a.*ptr; cout << "\npointer points memory address: " << a.ptrclass; cout << "\npointer points value: " << *a.ptrclass; getchar(); homecoming 0; } ques 1:-

why ptr accessible in main without reference class a, a.ptr as, have declared ptr int a:: *ptr; doesn't create ptr class fellow member ?

ques 2:-

when run above program, noticed, memory address of n of class different when fetched ptr, , a.ptrclass. didn't that. explainations please.

the questions might basic, please help.

why ptr accessible in main without reference class a, a.ptr as, have declared ptr int a:: *ptr; doesn't create ptr class fellow member ?

int a::* ptr;

does not declare ptr fellow member of class a. rather declares ptr pointer info fellow member of class a of type int. example:

class { public: int i0; int i1; }; int main() { int a::* mptr; mptr = &a::i0; mptr = &a::i1; }

pointer-to-members not point specific memory address, they're much offsets. example:

int main() { a[2] = {{1, 2}, {3, 4}}; int a::* mptr = &a::i0; // print first info fellow member (i0) of a[0] , a[1] for(int = 0; < 2; ++i) { std::cout << a[i].*mptr << "\n"; } mptr = &a::i1; // print sec info fellow member (i1) of a[0] , a[1] for(int = 0; < 2; ++i) { std::cout << a[i].*mptr << "\n"; } }

a possible memory layout of objects of class a:

class { public: int i0; int i1; }; a = {42, 21}; +-a--------------+ | +-i0-+ +-i1-+ | | | 42 | | 21 | | | +----+ +----+ | +----------------+

now, add together pointers:

int* pi1 = &a.i1; int a::* mpi1 = &a::i1; a* pa = &a; +-a--------------+ | +-i0-+ +-i1-+ | | | 42 | | 21 | | | +----+ +----+ | +---------^------+ ^ | pi1 | pa

both pi1 , pa point specific objects. on other hand, mpi1 offset an (= any) object of type a:

+-a--------------+ | +-i0-+ +-i1-+ | | | | | | | | +----+ +----+ | ~~~~~~~~~~^ mpi1 | +----------------+

we can combine mpi1 pa specific object a.i1:

+-a--------------+ | +-i0-+ +-i1-+ | | | 42 | | 21 | | | +----+ +----+ | +---------^------+ ^~~~~~~~~~+ mpi1 | pa

this written as:

pa->*mpi1 // look refers `a.i1` aka `*pi1`

so can identify:

&(pa->*mpi1) == pi1

if interpret addresses , offset integer values, might like:

pa == 0x7fff39519328 pi1 == 0x7fff3951932c mpi1 == 4

therefore, can see 8 + 4 == 12 == 0xc, is, "pa + mpi1 == pi1". not take literally, isn't allowed in c++ perform addition: pa + mpi1 not legal c++. additionally, isn't guaranteed platforms can add together 2 values. explains popular implementation on x86.

when run above program, noticed, memory address of n of class different when fetched ptr, , a.ptrclass. didn't that. explainations please.

cout << "\npointer points memory address: " << ptr;

this line does

cout << "\npointer points memory address: " << (bool)ptr;

which same as

cout << "\npointer points memory address: " << (ptr != nullptr);

to see this, try

cout << boolalpha;

which prints boolean values strings true / false.

on other hand, line:

cout << "\npointer points memory address: " << a.ptrclass;

does conversion void*. the raw memory pointer type in c++. every pointer specific object can converted void*, pointing same memory location. cout << some_void_star operation prints address of raw memory void* points to. many hardware architectures, address is, or can represented as, integer. integer representation typically printed operation cout << some_void_star.

pointer-to-members not pointers specific objects , hence not allowed converted void* (they not point specific address). printing value interpreted integer quite tricky. can try:

#include <algorithm> //... // code produce wrong results on big endian architectures static_assert(sizeof(unsigned long long) >= sizeof(mpi1)); unsigned long long as_integer; std::copy_n(reinterpret_cast<char const*>(&mpi1), sizeof(mpi1), reinterpret_cast<char*>(&as_integer)); std::cout << as_integer;

c++ class pointers

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -