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

G

gob00st

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
 
R

red floyd

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?
 
G

gob00st

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
 
G

gob00st

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...
 
G

gob00st

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
 
S

SG

        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
 
G

gob00st

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
 
C

Christof Donat

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
 
S

Sana

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
 
G

gob00st

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


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

Yes, its works. Thanks.
 
S

Sana

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


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
 
G

gob00st

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

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
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top