Hi,
re************* *@yahoo.com wrote:
Interesting. The type of X::array is a member of X which is an array
of 10 doubles. But (dobule X::*) means a member of X which is a
double. I'm confused why the assignment works, but hey, it's all good
if it does!
It works. I tested it.
I'll try to tell you what it does..
We generally do not write pointers this way. I mean.. I never did that.
I used to write like this..
double *pArray = &xObj.array; // Gives the address assigned in the
memory for array of xObj.
but when you write like this..
double (X::* junk1)[10] = &X::array;
or like this..
double X::*junk1 = (double X::*)&X::array;
You can see that the value is 0x000000. it took lot of time to
understand this.
next I added one more variable in the structure.
struct X
{
double array[10];
int i;
};
int X::*pInt = &X::i;
now when you try to get a pointer to i, you get 0x000050.
(8 Bytes for double * 10) = 80 base 10 = 50 base 16 (hex)
what I have understood is, it gives the offset. as double array[10] is
the first variable to start, the offset is 0 (the offset of &X::array).
so what happens when you access with X object is, it adds the offset
with the address of the object.
ex:
xObj.*pInt = 10; // *(&xObj + pInt (offset of int i)) // some thing
like this
it worked in the same way for..
double X::*junk1 = (double X::*)&X::array;
double *temp = &(xObj.*junk 1);
temp got the starting address of double array[10] allocated in the
memory for xObj.
(xObj.*junk1)[0] = 1.0f; // crashes here
that's because.. pointer to member variables or functions cannot use
arithmetic operations.
(I read that in Thinking in C++). I don't know if that is the actual
reason. That's just a guess.
I'm confused why the assignment works, but hey, it's all good
if it does!
double X::*junk1 = (double X::*)&X::array;
why leave it? we must understand.
double (X::*junk1)[10] is the pointer to array of 10 doubles
double X::*junk1 is the pointer to 1 double.
ok what happens in (double X::*)&X::array;
it takes the starting address of array, when assigning, the L value
must be capable of taking one double not 10. That's why we needed type
casting.
Tell me if you have tried some thing else.
Plz write your name. I dont want to talk to a computer.
-- Murali Krishna