Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Class Member Access Operator -> Overloading

C++ Class Member Access Operator -> Overloading

C++ Operator Overloading and Function Overloading C++ Overloaded Operators and Overloaded Functions

The class member access operator (->) can be overloaded, but it is somewhat tricky. It is defined to give a class "pointer-like" behavior. The -> operator must be a member function. If the -> operator is used, the return type must be a pointer or an object of the class.

The -> operator is often used together with the pointer dereference operator * to implement "smart pointer" functionality. These pointers are objects whose behavior resembles that of normal pointers, with the only difference being that they perform other tasks when you access objects through them. For example, they automatically delete the object when the pointer is destroyed or when it points to another object.

The dereference operator -> can be defined as a unary postfix operator. That is, given a class:



class Ptr{

   //...

   X * operator->();

};

class Ptr 's object can be used to access X 's members, in a way very similar to how pointers are used. For example:



void f(Ptr p )

{

   p->m = 10 ; // (p.operator->())->m = 10

}

The statement p->m is interpreted as (p.operator->())->m. Similarly, the following example demonstrates how to overload the class member access operator ->.

Example

#include

<

iostream

#include

<

vector

using

namespace

std

;

// assume a real class

class

Obj

{

static

int

i

,

j

;

public

:

void

f

(

)

const

{

cout

<<

i

++ <<

endl

;

}

void

g

(

)

const

{

cout

<<

j

++ <<

endl

;

}

}

;

// static member definition

int

Obj

::

i

=

10

;

int

Obj

::

j

=

12

;

// implement a container for the class above

class

ObjContainer

{

vector

<

Obj

*>

a

;

public

:

void

add

(

Obj

obj

)

{

a

.

push_back

(

obj

)

;

// call standard methods of the vector

}

friend

class

SmartPointer

;

}

;

// implement smart pointer to access members of class Obj

class

SmartPointer

{

ObjContainer

oc

;

int

index

;

public

:

SmartPointer

(

ObjContainer

&

objc

)

{

oc

=

objc

;

index

=

0

;

}

// return value indicates end of list

bool

operator

++

(

)

// prefix version

{

if

(

index

=

oc

.

a

.

size

(

)

1

)

return

false

;

if

(

oc

.

a

[

++

index

]

==

0

)

return

false

;

return

true

;

}

bool

operator

++

(

int

)

// postfix version

{

return

operator

++

(

)

;

}

// overload operator ->

Obj

operator

->

(

)

const

{

if

(

!

oc

.

a

[

index

]

)

{

cout

<<

"

Zero value

"

;

return

(

Obj

)

0

;

}

return

oc

.

a

[

index

]

;

}

}

;

int

main

(

)

{

const

int

sz

=

10

;

Obj

o

[

sz

]

;

ObjContainer

oc

;

for

(

int

i

=

0

;

i

<

sz

;

i

++

)

{

oc

.

add

(

&

o

[

i

]

)

;

}

SmartPointer

sp

(

oc

)

;

// create an iterator

do

{

sp

->

f

(

)

;

// smart pointer call

sp

->

g

(

)

;

}

while

(

sp

++

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



10

12

11

13

12

14

13

15

14

16

15

17

16

18

17

19

18

20

19

21

C++ Operator Overloading and Function Overloading C++ Overloaded Operators and Overloaded Functions