overloading operator ->*

  • Thread starter n2xssvv g02gfr12930
  • Start date
G

Gianni Mariani

n2xssvv said:
Does anyone know of an example of overloading operator ->*

Austria C++ has "smart pointers" that do this.

The operator needs to return a pointer.

struct T
{
int x;
};

struct X
{

T * m_ptr;

T * operator->()
{
return m_ptr;
}
};
 
N

n2xssvv g02gfr12930

Gianni said:
Austria C++ has "smart pointers" that do this.

The operator needs to return a pointer.

struct T
{
int x;
};

struct X
{

T * m_ptr;

T * operator->()
{
return m_ptr;
}
};

That's not an example of overloading 'operator ->*' but 'operator ->'.

JB
 
J

John Harrison

ben said:
How do you overload two operators at the same time???
Ben

->* is only one operator, just like .*

The are used with 'pointers to members'. Look them up in your favourite
C++ book.

john
 
G

Gianni Mariani

n2xssvv said:
That's not an example of overloading 'operator ->*' but 'operator ->'.

Yep, you're right. My mistake.


struct S
{
int z;

int & operator ->* ( int S::* const & x )
{
return this->*x;
}

};

struct Y
{
int z;

template <typename T>
T & operator ->* ( T Y::* const & x )
{
return this->*x;
}

};

int main()
{
S s;
int S::* pmz = &S::z;

s->*( pmz ) = 3;

Y y;
int Y::* pmy = &Y::z;

y->*( pmy ) = 3;
}
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top