member pointer to struct data

Discussion in 'C++' started by Allen, Sep 16, 2003.

  1. Allen

    Allen Guest

    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
     
    Allen, Sep 16, 2003
    #1
    1. Advertising

  2. 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";

    }
     
    Gianni Mariani, Sep 16, 2003
    #2
    1. Advertising

  3. Allen

    David White Guest

    Allen <allen-terri-ng$> wrote in message
    news:dDv9b.140766$...
    > 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
     
    David White, Sep 16, 2003
    #3
  4. Allen

    Allen Guest

    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?
     
    Allen, Sep 16, 2003
    #4
  5. 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.
     
    Gianni Mariani, Sep 16, 2003
    #5
  6. Allen

    David White Guest

    Allen <allen-terri-ng$> wrote in message
    news:uXw9b.140832$...
    > 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
     
    David White, Sep 16, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. beetle
    Replies:
    2
    Views:
    926
    beetle
    Jan 25, 2005
  2. Zero
    Replies:
    16
    Views:
    667
    Barry Schwarz
    Nov 19, 2005
  3. Stephen Howe
    Replies:
    2
    Views:
    298
    Stephen Howe
    Nov 6, 2012
  4. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    485
    Shao Miller
    Feb 20, 2013
  5. somenath
    Replies:
    10
    Views:
    293
    James Kanze
    Jul 2, 2013
Loading...

Share This Page