5 Replies - 859 Views - Last Post: 22 February 2012 - 08:51 PM Rate Topic: -----

#1 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:01 PM

For the life of me I can't see how to apply a deep copy to a class and class template. I need to make a function called deepcopy and its is a private function in a class template. It copies each non-pointer attribute directly from the object passed in the parmeter list. With the pointer attributes, it allocates a new array with the new operator and then in a loop, copies each member of the array passed into the function to the new array. Anyone have a simple example of how to apply a deepcopy?

#include <string>
#include <iostream>
using namespace std;
#ifndef LIST_H
#define LIST_H

template<class DT>
class List
{
	private:
		DT* pList;				// class attributes
		int numberInList;
		int listSize;
		int nextIndex;
	public:
		List();			
		List(int);
		List(const List<DT>&);
		~List();			
		void addMember(DT&); 
		bool getMember(DT&);
		DT getNext();
		int getNumOfMembers();
};
#endif

template<class DT>
List<DT>::List()				// default constructor
{
	pList = new DT [100];		// dynamically allocates 100 entries
	numberInList = 0;
	listSize = 100;
	nextIndex = 0;
}

template <class DT>
List<DT>::List(int n)			// overloaded constructor
{
	pList = new DT [n];			// sets listSize to the values passed
	numberInList = 0;			// and dynamically allocates array to that size
	listSize = n;
	nextIndex = 0;
}

template <class DT>				// overloaded assignemnt operator for List class
List<DT>& List<DT>::operator = (const List<DT>& right)
{
	if (this == &right)
		return *this;
		
	delete [] pList;
	deepCopy(right);
	
	return *this;
	
}

template<class DT>				// destructor freeing memory dynamically allocated
List<DT>::~List()
{
	cout<<"The destructor is running"<<endl;
	delete [] pList;
}

template<class DT>
void List<DT>::addMember(DT& member)
{
	pList[numberInList]=member;
	numberInList++;
}

template<class DT>
DT List<DT>::getNext()
{
	if (nextIndex>=numberInList)
		nextIndex=0;
	return pList[nextIndex++];
}

template<class DT>
bool List<DT>::getMember(DT& member)
{
	bool found = false;
	for(int i=0; i<numberInList; i++)
	{
		if(pList[i]==member)
		{
			member=pList[i];
			found=true;
		}
	}
	return found;
}

template<class DT>
int List<DT>::getNumOfMembers()
{
	return numberInList;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with deep copy/copy constructor

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:33 PM

Given that you've already written that entire template, what is it about your description of deepcopy that's causing your confusion?

You already have all of the essential elements in your existing code; it's just a matter of pulling them together.
Was This Post Helpful? 0
  • +
  • -

#3 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

Re: Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:33 PM

I need to add a deepCopy function as a private function within the List class template, and I have no idea how to start that.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:42 PM

Do you know how to copy numberInList and listSize from the argument to "this" (the lhs object)?
Was This Post Helpful? 0
  • +
  • -

#5 gwaihir  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 46
  • Joined: 06-April 11

Re: Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:46 PM

I don't know how to do that. We don't need a "this" for either of those.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Having trouble with deep copy/copy constructor

Posted 22 February 2012 - 08:51 PM

By "this" I simply mean the "current" object, i.e., the object on the left-hand side of the assignment operator.

The function that you're writing receives a parameter -- the object on the right-hand side of the assignment operator. Are you saying that you don't know how to access the value of one of the member variables of that object?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1