Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member pointer to struct data

Reply
Thread Tools

member pointer to struct data

 
 
Allen
Guest
Posts: n/a
 
      09-16-2003
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?
--

Best wishes,
Allen




 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-16-2003
Allen wrote:
> 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";

}

 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      09-16-2003
Allen <allen-terri-ng$(E-Mail Removed)> wrote in message
news:dDv9b.140766$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      09-16-2003
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?


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      09-16-2003
Allen wrote:
> 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.




 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      09-16-2003
Allen <allen-terri-ng$(E-Mail Removed)> wrote in message
news:uXw9b.140832$(E-Mail Removed)...
> 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?


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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
(: Pointer to struct withing pointer to struct :) Zero C Programming 16 11-19-2005 01:27 AM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments