Double Pointer.

M

Muzammil

Hi.Can any one help me using double pointer with template on any
class.
i m confuse in using double pointer.its my class .
//dynamic 2-d array class
template <class t>
class dsarray
{
private :
unsigned int size;
t** data;
public:

dsarray():data(NULL),size(0){}

dsarray(int s):data(new t*),size(s)
{
for(int i =0;i<size;i++)data=new t[size];
// memset(this->data,0,this->size*sizeof(t));
}
void getdata()
{
for (int i = 0;i < this->size*size;i++)
{
cout<<"Element # "<<i;
cin>>this->*data;
}
}
}
int main()
{
dsarray<int> d1(2),d3(3);
d1.getdata();
system("pause");
return 0;
}

here getdata(); have the problem in reading the input.
 
I

Ian Collins

Muzammil said:
Hi.Can any one help me using double pointer with template on any
class.
i m confuse in using double pointer.its my class .
//dynamic 2-d array class

Why don't you just use a vector or vectors?
 
M

Muzammil

i have to implement this .beacuse we are learning about double
pointer(new in data structure course.in OOP we didn't pratise of it
more.so now we have to work out.).
 
U

Uwe Schmitt

Hi.Can any one help me using double  pointer with template on any
class.
i m confuse in using  double pointer.its my class .
//dynamic 2-d array class
template <class t>
class dsarray
{
      private :
            unsigned int  size;
             t** data;
       public:

       dsarray():data(NULL),size(0){}

       dsarray(int s):data(new t*),size(s)


here you allocate memory,
       {
       for(int i =0;i<size;i++)data=new t[size];


and here again. this will result in memory leaks.
you will need a destrutor too. but i do not think that
this
    //   memset(this->data,0,this->size*sizeof(t));
       }
 void getdata()
       {
            for (int i = 0;i  < this->size*size;i++)
            {
                cout<<"Element # "<<i;
                cin>>this->*data;
            }
        }}

int main()
{
    dsarray<int> d1(2),d3(3);
    d1.getdata();
   system("pause");
    return 0;

}

here  getdata(); have the problem in reading the  input.


what do you mean ? what problem do you have ?

Greetings, Uwe
 
U

Uwe Schmitt

Hi.Can any one help me using double  pointer with template on any
class.
i m confuse in using  double pointer.its my class .
//dynamic 2-d array class
template <class t>
class dsarray
{
      private :
            unsigned int  size;
             t** data;
       public:
       dsarray():data(NULL),size(0){}
       dsarray(int s):data(new t*),size(s)


here you allocate memory,
       {
       for(int i =0;i<size;i++)data=new t[size];


and here again. this will result in memory leaks.


ups. i was to fast. no memory leak.
you will need a destrutor too. but i do not think that
this


    //   memset(this->data,0,this->size*sizeof(t));
       }
 void getdata()
       {
            for (int i = 0;i  < this->size*size;i++)
            {
                cout<<"Element # "<<i;
                cin>>this->*data;
            }
        }}

int main()
{
    dsarray<int> d1(2),d3(3);
    d1.getdata();
   system("pause");
    return 0;

here  getdata(); have the problem in reading the  input.

what do you mean ? what problem do you have ?

Greetings, Uwe
 
M

Muzammil

thanks. i know i haven't make a destructor.

Paavo said:
Muzammil said:
Hi.Can any one help me using double pointer with template on any
class.
i m confuse in using double pointer.its my class .
//dynamic 2-d array class
template <class t>
class dsarray
{
private :
unsigned int size;
t** data;
public:

dsarray():data(NULL),size(0){}

dsarray(int s):data(new t*),size(s)


Here, data is allocated as an array of size t* pointers.
{
for(int i =0;i<size;i++)data=new t[size];

// memset(this->data,0,this->size*sizeof(t));
}
void getdata()
{
for (int i = 0;i < this->size*size;i++)


Here, i goes from 0 to size*size.
{
cout<<"Element # "<<i;
cin>>this->*data;


"this->" is superfluous.

Here, data has been allocated only size elements, so it will overflow
soon when i grows larger than size. Also, each data points to an array
of t-s, but you only attempt to store something in its first element only
(the * operator dereferences the array pointer, which gives you its first
element only).

Missing semicolon. Also, your class does not have a proper destructor and
will leak memory.
int main()
{
dsarray<int> d1(2),d3(3);
d1.getdata();
system("pause");
return 0;
}

here getdata(); have the problem in reading the input.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top