What is overloading operator-> for?

I

Immortal Nephi

Can you please tell me what overloading operator-> is used for? My
code does not sound right. And also what is overloading operator->*
for? I am unable to find more information in the Google search.

class test
{
public:
test() : data( 0 ) { pF = &test::F1; }
~test() {}

test *operator->()
{
data++;
return this;
}

void (test::*pF)();

void Run() {}
void F1() {}
int data;
};


int main()
{
test t, *pt = &t;
t->Run();

return 0;
 
R

Rolf Magnus

Immortal said:
Can you please tell me what overloading operator-> is used for?

Mostly smart pointers and iterators, i.e. objects that are supposed to be
used similar to pointers.
My code does not sound right. And also what is overloading operator->*
for?

Calling member functions through pointer-to-member.
class test
{
public:
test() : data( 0 ) { pF = &test::F1; }
~test() {}

test *operator->()
{
data++;
return this;
}

There's not much point in returning this in an operator->. Normally, you
want to return a pointer to a different object.
 
Ö

Öö Tiib

        Can you please tell me what overloading operator-> is used for?  My
code does not sound right.

Usually it is reverse to the unary operator & and similar to unary
operator *. With inbuilt unary & you get pointer to your object, with -
and unary * you get the object pointed at by pointer. These are best
to be overloaded only when you are dealing with class that acts like
pointer.
 And also what is overloading operator->*
for?  I am unable to find more information in the Google search.

Pointer to member is rarely used directly in code. It's used behind
scenes in some templates. That is why you get no much examples of
overloading it. It is used rarely and so it is better to be leave not
overloaded. You may badly confuse someone who tries to understand your
code.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top