member pointer to struct data

A

Allen

Hi all,

I didn't find this explicitly stated in the FAQ so I wanted to verify
it.

A class and a struct are the same except for their default access
levels, right? So then, if I wanted a member pointer in my class to point
to an int in a struct I would have to declare the member pointer to be:

class MyClass
{
MyStruct::int* piMyIntPntr;
};

Is that correct?
 
G

Gianni Mariani

Allen said:
Hi all,

I didn't find this explicitly stated in the FAQ so I wanted to verify
it.

A class and a struct are the same except for their default access
levels, right? So then, if I wanted a member pointer in my class to point
to an int in a struct I would have to declare the member pointer to be:

class MyClass
{
MyStruct::int* piMyIntPntr;
};

Is that correct?

Not quite - try this:
int MyStruct::*

here is an example I whipped up:

#include <iostream>

struct A
{
int z1;
int z2;
};


class B
{
public:

int A::* zp;
A * p;

B( int A::* zpp, A * v )
: zp( zpp ),
p( v )
{
}

int Rv()
{
return p->*zp;
}
};


int main()
{

A a;
a.z1 = 88;
a.z2 = 99;

std::cout << B(&A::z1, &a).Rv() << "\n";

std::cout << B(&A::z2, &a).Rv() << "\n";

}
 
D

David White

Allen said:
Hi all,

I didn't find this explicitly stated in the FAQ so I wanted to verify
it.

A class and a struct are the same except for their default access
levels, right?

Yes, for both member access and inheritance.
So then, if I wanted a member pointer in my class to point
to an int in a struct I would have to declare the member pointer to be:

This could be interpreted two ways, either as a genuine pointer-to-member,
or as an ordinary member pointer that just happens to point to a member int
of some object. Since Gianni has already covered the pointer-to-member case,
I'll leave that out.
class MyClass
{
MyStruct::int* piMyIntPntr;
};

For the ordinary pointer case (stealing Gianni's code, with mods):

#include <iostream>

struct A
{
int z1;
int z2;
};


class B
{
public:
int * zp;
A * p;

B( int * zpp, A * v )
: zp( zpp ),
p( v )
{
}

int Rv()
{
return *zp;
}
};


int main()
{

A a;
a.z1 = 88;
a.z2 = 99;

std::cout << B(&a.z1, &a).Rv() << "\n";

std::cout << B(&a.z2, &a).Rv() << "\n";

}

I realize that this might not have been what you had in mind, and it might
not be suitable for your purpose, but the pointer has the advantage in being
able to point to any int anywhere, not just to an int member of a specific
class.

DW
 
A

Allen

Hi David, Gianni, all,
#include <iostream>

struct A
{
int z1;
int z2;
};


class B
{
public:
int * zp;
A * p;

B( int * zpp, A * v )
: zp( zpp ),
p( v )
{
}

int Rv()
{
return *zp;
}
};


int main()
{

A a;
a.z1 = 88;
a.z2 = 99;

std::cout << B(&a.z1, &a).Rv() << "\n";

std::cout << B(&a.z2, &a).Rv() << "\n";

}

I realize that this might not have been what you had in mind, and it might
not be suitable for your purpose, but the pointer has the advantage in being
able to point to any int anywhere, not just to an int member of a specific
class.

This is actually very close to what I've got. I think my error was
(using this example) taking the address of z1 instead of a.z1.

Couple of follow-on questions:
1) Would &a.z1 be affected by being cast to a void* and then back?

2) Just curious. What is the format of a pointer to member? I know it has
to have storage for the this pointer, but how is that stored?
 
G

Gianni Mariani

Allen said:
Hi David, Gianni, all,
....

This is actually very close to what I've got. I think my error was
(using this example) taking the address of z1 instead of a.z1.

Couple of follow-on questions:
1) Would &a.z1 be affected by being cast to a void* and then back?

You can cast any pointer to a void * and back to that same pointer type
and not loose anything.
2) Just curious. What is the format of a pointer to member? I know it has
to have storage for the this pointer, but how is that stored?

That's implementation defined however, it's usually the same size as a
data pointer. (i.e. 32 bit number for 32 bit pointers). It basically
stores the offset of the member from the beginning of the object.
 
D

David White

Allen said:
Hi David, Gianni, all,


This is actually very close to what I've got. I think my error was
(using this example) taking the address of z1 instead of a.z1.

Couple of follow-on questions:
1) Would &a.z1 be affected by being cast to a void* and then back?

No. Of course, it's better to avoid casts completely if you can.
2) Just curious. What is the format of a pointer to member? I know it has
to have storage for the this pointer, but how is that stored?

In the case of data members, it's a kind of offset. I don't know what the
standard says, but I imagine the compiler uses some kind of integer type
that holds the offset within an object of the specified member. This is why
the code I posted is not always suitable. With a pointer to member you can
access the same member of any object of the specified class with the same
"pointer" to member, which is sometimes what you need. You can't do that
with my code. If you look at Gianni's code you'll see that the value of the
pointer-to-member was determined without reference to an actual object.

DW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top