Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C
Expertise: Beginner
Nov 20, 2003



Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Make an Object Appear Like a Pointer

You can make an object appear like a pointer with a smart pointer. If a class overloads the operator ->( ), then any object of that class can appear like a pointer when the operator ->( ) is called. The following program illustrates this:

#include "iostream.h"
class test
public :
void fun( )
cout << "fun of smart pointer" ;
} ;

class smartpointer
test t ;
public :
test* operator ->( )
return &t ;
} ;

void main( )
smartpointer sp ;
sp -> fun( ) ;
The beauty of overloading operator ->( ) is that even though sp is an object, you can make it work like a pointer. The operator ->( ) returns the address of the object of the type test. Using this address of the test object the function fun( ) of the class test gets called. Thus even though fun( ) is not a member of smartpointer class, you can still call it using sp.
Murali Krishna
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date