has anyone seen this?

Discussion in 'C++' started by Amadeus W.M., Mar 10, 2007.

  1. Amadeus W.M.

    Amadeus W.M. Guest

    I saw somewhere some code that in a simplified version boils down to the
    example below. Could someone explain when one would define a pointer as
    that in main() below, and why? Pointers to documents that explain this
    will do. Thanks!


    class MyClass
    {
    public:
    int x;
    };


    int main()
    {
    int MyClass::*px = &MyClass::x;

    return 0;
    }
    Amadeus W.M., Mar 10, 2007
    #1
    1. Advertising

  2. * Amadeus W.M.:
    > I saw somewhere some code that in a simplified version boils down to the
    > example below. Could someone explain when one would define a pointer as
    > that in main() below, and why? Pointers to documents that explain this
    > will do. Thanks!
    >
    >
    > class MyClass
    > {
    > public:
    > int x;
    > };
    >
    >
    > int main()
    > {
    > int MyClass::*px = &MyClass::x;
    >
    > return 0;
    > }


    Usually there's no good reason to use pointers to members.

    Any pointer to member can be represented instead as a pointer to a
    function, like

    struct MyClass{ int x; };

    int& xMember( MyClass& o ) { return o.x; }

    int main()
    {
    int (*pfx)( MyClass& ) = &xMember;
    }

    Well, at least I think the "any" holds. Pointer to members are so
    rarely used that I can't think of an example where the "any" doesn't
    hold. Although such an example may exist.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 10, 2007
    #2
    1. Advertising

  3. Amadeus W.M.

    Phlip Guest

    Amadeus W.M. wrote:

    > I saw somewhere some code that in a simplified version boils down to the
    > example below. Could someone explain when one would define a pointer as
    > that in main() below, and why? Pointers to documents that explain this
    > will do. Thanks!


    Google [C++ faq member pointer]

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
    Phlip, Mar 10, 2007
    #3
    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. KatB
    Replies:
    1
    Views:
    348
    Bob Lehmann
    Oct 24, 2003
  2. Replies:
    3
    Views:
    474
    Juan T. Llibre
    Nov 4, 2005
  3. =?Utf-8?B?QW5kcmV3?=

    Anyone has seen this error?

    =?Utf-8?B?QW5kcmV3?=, Feb 7, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    441
    Karl Seguin [MVP]
    Feb 7, 2006
  4. jens Jensen

    Has anyone seen this before?

    jens Jensen, May 2, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    295
    jens Jensen
    May 2, 2006
  5. Replies:
    8
    Views:
    850
Loading...

Share This Page