overloading operator->*() and operator->()

Discussion in 'C++' started by gob00st@googlemail.com, Feb 20, 2009.

  1. Guest

    Can anyone give me a example of how to overload those two operators
    and basic usage please ?
    And the what is the difference between operator->*() , operator->()
    and operator.() , operator.*() (those two can't be overloaded).

    Thanks in advance.
    Gob00st
    , Feb 20, 2009
    #1
    1. Advertising

  2. red floyd Guest

    On Feb 20, 7:48 am, wrote:
    > Can anyone give me a example of how to overload those two operators
    > and basic usage please ?
    > And the what is the difference between operator->*() , operator->()
    > and operator.() , operator.*() (those two can't be overloaded).
    >


    What book are you reading that doesn't discuss pointers to members?
    red floyd, Feb 20, 2009
    #2
    1. Advertising

  3. Guest

    On Feb 20, 4:39 pm, red floyd <> wrote:
    > On Feb 20, 7:48 am, wrote:
    >
    > > Can anyone give me a example of how to overload those two operators
    > > and basic usage please ?
    > > And the what is the difference between operator->*() , operator->()
    > > and operator.() , operator.*() (those two can't be overloaded).

    >
    > What book are you reading that doesn't discuss pointers to members?


    MSDN...

    Why those two people above just ask me question without trying to help
    a bit?
    Of course its pointers to members operator ,but I just trying to find
    a example of overloading them.

    So any input is appreciated...
    Gob00st
    , Feb 20, 2009
    #3
  4. Guest

    On Feb 20, 4:55 pm, wrote:
    > On Feb 20, 4:39 pm, red floyd <> wrote:
    >
    > > On Feb 20, 7:48 am, wrote:

    >
    > > > Can anyone give me a example of how to overload those two operators
    > > > and basic usage please ?
    > > > And the what is the difference between operator->*() , operator->()
    > > > and operator.() , operator.*() (those two can't be overloaded).

    >
    > > What book are you reading that doesn't discuss pointers to members?

    >
    > MSDN...
    >
    > Why those two people above just ask me question without trying to help
    > a bit?
    > Of course its pointers to members operator ,but I just trying to find
    > a example of overloading them.
    >
    > So any input is appreciated...
    > Gob00st


    So any input other than complaining / sarcasm is appreciated...
    , Feb 20, 2009
    #4
  5. Guest

    On Feb 20, 5:24 pm, Jeff Schwab <> wrote:
    > wrote:
    > > On Feb 20, 4:55 pm, wrote:
    > >> On Feb 20, 4:39 pm, red floyd <> wrote:

    >
    > >>> On Feb 20, 7:48 am, wrote:
    > >>>> Can anyone give me a example of how to overload those two operators
    > >>>> and basic usage please ?
    > >>>> And the what is the difference between operator->*() , operator->()
    > >>>> and operator.() , operator.*() (those two can't be overloaded).
    > >>> What book are you reading that doesn't discuss pointers to members?
    > >> MSDN...

    >
    > >> Why those two people above just ask me question without trying to help
    > >> a bit?
    > >> Of course its pointers to members operator ,but I just trying to find
    > >> a example of overloading them.

    >
    > >> So any input is appreciated...
    > >> Gob00st

    >
    > > So any input other than complaining / sarcasm is appreciated...

    >
    > It's basically just a pain in the neck to explain, and isn't something
    > that comes up often enough for people to have examples at hand.  If this
    > is a purely academic question, Google is probably your best bet.  If,
    > OTOH, you have a real-world situation that needs a practical answer, it
    > would be helpful if you could explain it.


    I admit its may not a practical question since I have never overloaded
    these two operators before.
    That's why I am curious enough to post this thread to see if anyone
    have actually had any practical usage them.

    Here is my tentative code of overloading operator->*(), it works , but
    its ugly. Any one know how to use it correctly?
    class Base;
    typedef void (Base::*PMF) (int);

    class Base {
    public:
    void foo(int a)
    {
    cout << "base::foo" << endl;
    }

    void operator->*(PMF p)
    {
    cout << "ponter to member operator overloaded" << endl;
    (this->*p)(1);
    }
    };

    int main()
    {
    PMF pmf;
    Base b;
    pmf = &Base::foo;
    Base* pb = &b;
    pb->operator->*(pmf); //here my overloading operator->* has been
    called, but its ugly
    (pb->*pmf)(1); //why this doesn't invoke my overloaded operator-
    >*

    return 0;
    }

    Thanks.
    Gob00st
    , Feb 20, 2009
    #5
  6. SG Guest

    On 20 Feb., 18:51, wrote:
    >         Base* pb = &b;
    >         pb->operator->*(pmf);
    >         (pb->*pmf)(1);


    You overloaded ->* on base. But the type of "pb" is of "base*".
    That's why.

    Cheers!
    SG
    SG, Feb 20, 2009
    #6
  7. Guest

    On Feb 20, 6:22 pm, SG <> wrote:
    > On 20 Feb., 18:51, wrote:
    >
    > >         Base* pb = &b;
    > >         pb->operator->*(pmf);
    > >         (pb->*pmf)(1);

    >
    > You overloaded ->* on base. But the type of "pb" is of "base*".
    > That's why.
    >
    > Cheers!
    > SG


    Thanks SG, it's the most constructive reply in this thread.
    But operator->*() requires it first argument to be a pointer to the
    type which is overloading.
    Can I can not invoke operator->* with b.

    I can only write (b.*pmf)(1) with object b which should be calling
    operator.*() ,but it cannot be overloaded. I am getting confused.

    What is the correct overloading of operator->*() and usage?
    Thanks,
    Gob00st
    , Feb 20, 2009
    #7
  8. Hi,

    > Can anyone give me a example of how to overload those two operators
    > and basic usage please ?
    > And the what is the difference between operator->*() , operator->()
    > and operator.() , operator.*() (those two can't be overloaded).


    operator->() is usually used for stuff like smart pointers:

    Christof
    Christof Donat, Feb 20, 2009
    #8
  9. Sana Guest

    On Feb 20, 2:17 pm, wrote:
    > On Feb 20, 6:22 pm, SG <> wrote:
    >
    > > On 20 Feb., 18:51, wrote:

    >
    > > >         Base* pb = &b;
    > > >         pb->operator->*(pmf);
    > > >         (pb->*pmf)(1);

    >
    > > You overloaded ->* on base. But the type of "pb" is of "base*".
    > > That's why.

    >
    > > Cheers!
    > > SG

    >
    > Thanks SG, it's the most constructive reply in this thread.
    > But operator->*() requires it first argument to be a pointer to the
    > type which is overloading.


    SG is right. You overloaded operator->* for the type not for the
    _pointer_to_type_ (actually you cannot do that) so you have to use
    operator->* with an object and not a pointer


    > Can I can not invoke operator->* with b.


    yes, you can:

    b->*pmf;

    those operators (-> and ->*) are usually used with objects that must
    behave like pointers. such an example would be a smart pointer

    --
    sana
    Sana, Feb 20, 2009
    #9
  10. Guest

    On Feb 20, 8:18 pm, Sana <> wrote:
    > On Feb 20, 2:17 pm, wrote:
    >
    >
    >
    >
    >
    > > On Feb 20, 6:22 pm, SG <> wrote:

    >
    > > > On 20 Feb., 18:51, wrote:

    >
    > > > >         Base* pb = &b;
    > > > >         pb->operator->*(pmf);
    > > > >         (pb->*pmf)(1);

    >
    > > > You overloaded ->* on base. But the type of "pb" is of "base*".
    > > > That's why.

    >
    > > > Cheers!
    > > > SG

    >
    > > Thanks SG, it's the most constructive reply in this thread.
    > > But operator->*() requires it first argument to be a pointer to the
    > > type which is overloading.

    >
    > SG is right. You overloaded operator->* for the type not for the
    > _pointer_to_type_ (actually you cannot do that) so you have to use
    > operator->* with an object and not a pointer
    >
    > > Can I can not invoke operator->* with b.

    >
    > yes, you can:
    >
    > b->*pmf;
    >
    > those operators  (-> and ->*) are usually used with objects that must
    > behave like pointers. such an example would be a smart pointer
    >
    > --
    > sana- Hide quoted text -
    >
    > - Show quoted text -


    Yes, its works. Thanks.
    , Feb 20, 2009
    #10
  11. Sana Guest

    On Feb 20, 3:18 pm, Sana <> wrote:
    > On Feb 20, 2:17 pm, wrote:
    >
    >
    >
    > > On Feb 20, 6:22 pm, SG <> wrote:

    >
    > > > On 20 Feb., 18:51, wrote:

    >
    > > > >         Base* pb = &b;
    > > > >         pb->operator->*(pmf);
    > > > >         (pb->*pmf)(1);

    >
    > > > You overloaded ->* on base. But the type of "pb" is of "base*".
    > > > That's why.

    >
    > > > Cheers!
    > > > SG

    >
    > > Thanks SG, it's the most constructive reply in this thread.
    > > But operator->*() requires it first argument to be a pointer to the
    > > type which is overloading.

    >
    > SG is right. You overloaded operator->* for the type not for the
    > _pointer_to_type_ (actually you cannot do that) so you have to use
    > operator->* with an object and not a pointer
    >
    > > Can I can not invoke operator->* with b.

    >
    > yes, you can:
    >
    > b->*pmf;
    >
    > those operators  (-> and ->*) are usually used with objects that must
    > behave like pointers. such an example would be a smart pointer


    complete example with both operators:


    #include <iostream>

    class Base;
    typedef void (Base::*PMF) (int);

    struct inner
    {
    int a;

    inner() : a(10)
    {
    }
    };

    class Base
    {
    inner value;
    public:
    void foo(int a)
    {
    std::cout << "base::foo" << std::endl;
    }
    void operator->*(PMF p)
    {
    std::cout << "ponter to member operator overloaded" << std::endl;
    (this->*p)(1);
    }

    inner* operator->()
    {
    std::cout << "overloaded operator -> " << std::endl;
    return &value;
    }
    };

    int main()
    {
    PMF pmf;
    Base b;
    pmf = &Base::foo;

    b->*pmf; // operator->*

    int value = b->a; // operator->

    std::cout << "value is: " << value << std::endl;

    return 0;
    }

    the operator->() behaves a little bit different than you expected.

    when you write b->a; what actually happens is the following
    (b.operator->())->a

    so b->a first calls b.operator->() then on the returned value applies
    the operator-> to get to a

    have fun

    --
    sana
    Sana, Feb 20, 2009
    #11
  12. Guest

    On Feb 20, 8:33 pm, Sana <> wrote:
    > On Feb 20, 3:18 pm, Sana <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 20, 2:17 pm, wrote:

    >
    > > > On Feb 20, 6:22 pm, SG <> wrote:

    >
    > > > > On 20 Feb., 18:51, wrote:

    >
    > > > > >         Base* pb = &b;
    > > > > >         pb->operator->*(pmf);
    > > > > >         (pb->*pmf)(1);

    >
    > > > > You overloaded ->* on base. But the type of "pb" is of "base*".
    > > > > That's why.

    >
    > > > > Cheers!
    > > > > SG

    >
    > > > Thanks SG, it's the most constructive reply in this thread.
    > > > But operator->*() requires it first argument to be a pointer to the
    > > > type which is overloading.

    >
    > > SG is right. You overloaded operator->* for the type not for the
    > > _pointer_to_type_ (actually you cannot do that) so you have to use
    > > operator->* with an object and not a pointer

    >
    > > > Can I can not invoke operator->* with b.

    >
    > > yes, you can:

    >
    > > b->*pmf;

    >
    > > those operators  (-> and ->*) are usually used with objects that must
    > > behave like pointers. such an example would be a smart pointer

    >
    > complete example with both operators:
    >
    > #include <iostream>
    >
    > class Base;
    > typedef void (Base::*PMF) (int);
    >
    > struct inner
    > {
    >         int a;
    >
    >         inner() : a(10)
    >         {
    >         }
    >
    > };
    >
    > class Base
    > {
    >                 inner value;
    > public:
    >         void foo(int a)
    >         {
    >                         std::cout << "base::foo" << std::endl;
    >         }
    >         void operator->*(PMF p)
    >         {
    >                         std::cout << "ponter to member operator overloaded" << std::endl;
    >             (this->*p)(1);
    >         }
    >
    >                 inner* operator->()
    >         {
    >                         std::cout << "overloaded operator -> " << std::endl;
    >             return &value;
    >         }
    >
    > };
    >
    > int main()
    > {
    >         PMF pmf;
    >         Base b;
    >         pmf = &Base::foo;
    >
    >                 b->*pmf;                     // operator->*
    >
    >                 int value = b->a;    // operator->
    >
    >                 std::cout << "value is: " << value << std::endl;
    >
    >         return 0;
    >
    > }
    >
    > the operator->() behaves a little bit different than you expected.
    >
    > when you write b->a; what actually happens is the following
    > (b.operator->())->a
    >
    > so b->a first calls b.operator->() then on the returned value applies
    > the operator-> to get to a
    >
    > have fun
    >
    > --
    > sana- Hide quoted text -
    >
    > - Show quoted text -


    Thanks sana :)
    But the practical example is only for overlaoding operator->() for
    smart pointer:
    template <class T>
    class SmartPtr
    {
    public:
    explicit SmartPtr(T* pointee) : pointee_(pointee){}
    ~SmartPtr(){}

    T& operator*() const
    {

    return *pointee_;
    }

    T* operator->() const
    {

    return pointee_;
    }

    private:
    T* pointee_;
    };

    Is there any practical useage of operator->*() ?

    Gob00st
    , Feb 20, 2009
    #12
    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. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  2. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,404
    Fredrik Lundh
    Sep 30, 2005
  3. Replies:
    11
    Views:
    727
    James Kanze
    May 16, 2007
  4. hurcan solter
    Replies:
    3
    Views:
    723
    Cholo Lennon
    Aug 29, 2007
  5. Replies:
    2
    Views:
    306
Loading...

Share This Page