Smart pointer

I hope most of you Guys are familiar with the concept of linked list,tress in c++
in all these each node point to next element ...now these nodes may not be in adjacent memory locations ....which in turn leads to complicated process
so in order to avoid this we use concept of smart pointers

Before beginning ..Have a look of this code
#include
#include
int main()
{
const int max=5;
int a[]={10,20,30,40,50};
int *p;
p=arr;
for(int i=0;iThis is a simple method to use pointer to array.no problem for data structure like array ..but what  for sophisticated data structures like linked list ,trees etc these plain pointers won't work..so we use smart pointers..as in linked list items may not be stored in adjacent memory locations,,so incrementing the pointers becomes complicated 
this can solved by using this concept of 'smart pointers'
have a look of this class
class smartpointer
{
private:
int *p;
public:
int operator++()
{}
int operator*()
{}
};
the operators + & * are overloaded in this class to make it intelligent enough to handle such situations

Now there must be a question in your mind...
How and where to use????????😕😕
#include
#include
class container
{
private:
structnode
{
int node;
node *link;
}*head,*current;
int current;
public:
container()
{
head=current=Null;
count=0;
}
void add(int n)
{
node *temp=new node;
temp->data=n;
temp->link=NULL;
if(head==NULL)
head=current=temp;
else
{
node *q;
q=HEAD;
while(q->link!=NULL)
{
q=q->link;
q->link=temp;
}
count++;
}
int getcount()
{
return count;
}
freind class smartpointer;
};

class smartpointer
{
private:
  container *p;
public:
smartpointer(conatiner *t)
{
cptr=t;
}
int operator *()
{
if(cptr->current=NULL)
return NULL;
else
{
int i=cptr->current->data;
return i;
}
}
 void operator++()
{
if(cptr->current!=NULL}
cptr->current=cptr->current->link;
}
};
void main()
{
container c;
c.add(12);
c.add(12);
c.add(13);
c.add(!4);
c.add(15);
smartpointer(&c);
for?(int i=0;iHere in smart pointer we have two overloaded operator functions
which can be used to handle such situations of non adjacent memory locations
The operator *() returns a integer in the current node;
the operator ++() advances the current pointer to point to next node in the linked list

Replies

You are reading an archived discussion.

Related Posts

Calling all crazy engineers. Here is the situation. My brother (John) and I had a debate (OK, an argument) about whether a pipe from a dam on the hill would...
Look here: A New Year's gift to Engadget readers: 3 minutes of Woz -- Engadget The really crazy engineer who changed the world. Those who don't know Mr. Wozniak, let...
This ad was created by participants of the WAD at JIT Nagpur. Check out the ad that follows the presentation - Ad Campaigning - Pigamento Glue!!! I had a good...
Hello Engeez, Coppy - Multi-copy/paste software QREO is a start-up of my own, more talk about it (if you like) will be in another topic, in the meantime we released...
Mumbai: A group of people in Mumbai came launched a campaign named as 'No-Tie' to save energy and protect the environment, reports BBC. Campaigners believe that not wearing a tie...