an implementation for the Pointer class.

Main()
{
Pointer ptr = new int(10);
Pointer ptr2 = new char;
Pointer ptr3 = ptr2;
int I = *ptr;
}

writing a c++ pointer class, for the above input.
I dont know how to write this class.
The 3rd line ptr3=ptr2 is the copy constructor and the 4th line is assignment operator that i know to write.

I am confused with the first two lines.
Can anyone help me solving this.
typedef void *Pointer; (just a really bad idea)
Is this what you want to do?

1
2
3
4
5
6
7
8
9
10
11
template< typename T >
class Pointer
{
    public:
        typedef T* pointer_type;

        Pointer( pointer_type p ) : ptr( p ) {}

    private:
        pointer_type ptr;
};


The first two lines create a pointer to an integer and initialize the integer to 10 and create a pointer to
an uninitialized char.
thanks for the answer i got it now.
#include<iostream>
#include<cstring>
using namespace std;
class pointer
{
};

class charpointer:public pointer
{
public:
char *char_ptr;
charpointer(char * c)
{
char_ptr = c;
}

int operator !=(charpointer c)
{
int x;
x=strcmp(char_ptr, c.char_ptr);
if(x!=0)
return 1;
else

return 0;
}

friend ostream& operator<< (ostream &, charpointer *);
};

ostream& operator<< (ostream & output, charpointer *c)
{
output <<c->char_ptr<<endl;
return output;
}

class intpointer:public pointer
{
public:
int *int_ptr;

intpointer(int *i)
{
int_ptr=i;
}

int operator ==(intpointer &i)
{

if(int_ptr==i.int_ptr)
{
return 0;
}
return 1;

}

};

int main()
{
char *chptr1=new char('x');
char *chptr2=(char*)"Hello World";
pointer *ptr1=new charpointer(chptr1);
pointer *ptr2=new charpointer(chptr2);

if(ptr1!=ptr2)
{
cout<<"contents are not same"<<endl;
cout<<(charpointer*)ptr1;
cout<<(charpointer*)ptr2;
}

int i=10;
pointer *intptr1= new intpointer(&i);
int *p= new int(10);
pointer *intptr2= new intpointer(p);
intptr1==intptr2;

if(intptr1==intptr2)
cout<<"pointer address are same"<<endl;
return 0;
}



In the last (intptr1==intptr2) operator is not calling the overloaded == function,
Is there any thing wrong in the syntax.


In the last (intptr1==intptr2) operator is not calling the overloaded == function,
Is there any thing wrong in the syntax.
you compare the pointer (you named a class pointer which is really missleading -> don't). To compare the content you have to write ((*intptr1)==(*intptr2)) here
My question is "why is it not calling the overloaded == function", I can compare the pointer address inside the overloaded == function.
I am not getting any compile time error or run time error.
In the previous statements (ptr1!=ptr2), it is calling overloaded != operator and working fine.
the != operator does not get called - you are confused about what is going on here.

yah I am sorry != operator is not getting called
Topic archived. No new replies allowed.