Jump to content

Not sure how to make copy constructor [C++]

So I have 2 constructors, the default constructor sets some member variables to their default values. The other constructor I guess is supposed to make a  deep copy of the object you pass it. 

ArrayList::ArrayList() //DEFAULT CONSTRUCTOR
{
	capacity = 8;
	length = 0;
	array = new int[capacity];
	for(int i = capacity - 1 ; i > length; --i) 
	{
		array[i] = NULL;
	}
}

ArrayList::ArrayList(const ArrayList& other) //OTHER CONSTRUCTOR
{
	//capacity(other.capacity);
	//length(other.length);
}

I have tried googling an answer and I have gotten mixed answers, some say to do something like 'object b = a;' but that does not seem to work.

Link to comment
Share on other sites

Link to post
Share on other sites

ArrayList::ArrayList(const ArrayList& other)
{
	capacity = other.capacity;
	length = other.length;
	array = other.array;
	
}

ayy I think I am going somewhere

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, poppop said:

ArrayList::ArrayList(const ArrayList& other)
{
	capacity = other.capacity;
	length = other.length;
	array = other.array;
	
}

ayy I think I am going somewhere

 

Yeah. The idea is that you copy every attribute of the passed object such that any change made to one object won't affect the other, but any operation performed on one object will return an identical result with other.

Link to comment
Share on other sites

Link to post
Share on other sites

This answer on SO is very solid regarding the "big three". The basic is that if your class needs a custom constructor, you will probably need to create the copy/move constructors and the destructor. The main idea, as mentioned my Mr/Mrs SSL is to ensure the new object is a member-wise copy of the other object, but the new object doesn't take any of the others resources, which may lead to undefined behavior in the future. This is especially apparent if you allocate something on the heap (with the 'new' keyword) and try to 'delete' it later.

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, Pinguinsan said:

This answer on SO is very solid regarding the "big three". The basic is that if your class needs a custom constructor, you will probably need to create the copy/move constructors and the destructor. The main idea, as mentioned my Mr/Mrs SSL is to ensure the new object is a member-wise copy of the other object, but the new object doesn't take any of the others resources, which may lead to undefined behavior in the future. This is especially apparent if you allocate something on the heap (with the 'new' keyword) and try to 'delete' it later.

checks out with the rest of my code, I have the destructor + the copy constructor ^^^

20 hours ago, AlexTheRose said:

Why aren’t you just using an std::vector? Or is this some school assignment? O.o

yea it was an assignment else I would have used the vector

 

 

also for future reference, copying the array like my second post shows is wrong (program crashes sometimes), it should be 

	array =  new int[capacity];
	for(int i = 0; i < capacity; ++i)
	{
		array[i] = other.get(i);
	}

instead of 

array = other.array;

 

Link to comment
Share on other sites

Link to post
Share on other sites

Let's consider something like this :

class array
{
public:
array(int len) : len(len) 
{
ar = new int[len];
}

int& operator[](const int index)
{
return ar[index];
}

private:
int *ar, len;
};

And you want to do this :

array1 = array2;

You'll have to overload the assignment operator :

void operator=(const array other)
{
len = other.len;
for(int i = 0; i < len; ++i)
                      (*this)[i] = other[i];
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×