A new C ++ Template class constructor that takes a type of itself as an argument

I am working on a C ++ class template similar to ArrayList in java (yes, I know that the vector does the same, this is not a utility project).

I realized that it would be useful to have a constructor for my ArrayList class that takes another ArrayList as an argument for the ArrayList seed. But when I try to write a constructor, I get this error

invalid constructor; you probably meant 'ArrayList<T> (const ArrayList<T>&)' 

Does this mean that an ArrayList should be a constant? And why do I need an operator address?

I'm still learning C ++ ropes, so I'm a bit confused.

Prototypes here:

  ArrayList(ArrayList<T> list); ArrayList(ArrayList<T> list, int size); 

The code is here:

 /** * Creates an ArrayList of type T that is twice the * size of the passed in ArrayList, and adds all elements * from the passed ArrayList<T> list, to this ArrayList. * * Runs in O(n) time, where n = the size of list. * * @param list the ArrayList to use as a seed for this ArrayList. */ template<class T> ArrayList<T>::ArrayList(ArrayList<T> list) { array = new T[list.getSize() * 2]; capacity = list->getSize() * 2; size = list->getSize(); for (int i = 0; i < list->getSize(); i++) { array[i] = list->get(i); } } 

Edit There are no errors in the code below, while the above does .....

 /** * Creates an ArrayList of type T that has a capacity equal to the passed * in theCapacity parameter. This ArrayList starts with the passed ArrayList. * * Note: If the passed in capacity is smaller than the size of the passed in * ArrayList, then the capacity is set to twice the size of the * passed ArrayList. * * @param list the ArrayList to use as a seed for this ArrayList. * @param theCapacity the capacity for this ArrayList. */ template<class T> ArrayList<T>::ArrayList(ArrayList<T> list, int theCapacity) { if (theCapacity >= list->getSize()) { array = new T[theCapacity]; capacity = theCapacity; } else { array = new T[list->getSize() * 2]; capacity = list->getSize() * 2; } size = list->size; for (int i = 0; i < size; i++) { array[i] = list->get(i); } } 
+4
source share
2 answers

Using

  ArrayList<T>::ArrayList(const ArrayList<T>& list) 

as your constructor signature, to pass arraylist as a const reference. This is the correct signature for the copy constructor. Both the implementation code and the call code should not change unless you change the list inside your ctor.

When you make an ArrayList<T>::ArrayList(ArrayList<T> list) , you create a temporary copy of the entire ArrayList instance. (You should not do this for ctors, since it will cause infinite recursion, since a new copy of list will use the same function definition, etc.).

ArrayList<T>::ArrayList(ArrayList<T>& list) passes the list as a reference, that is, it is no longer what is often called "pass by value", and works on the exact version of the list from the calling code.

Accepting a reference to a constant in a function, you say that the function will not change the contents of the link (i.e. it will not perform any modifying operations on it: it will be limited only by const access). You should read about const , references, and copy constructors before moving on.

Update:

To pass by value or reference, you can access the elements through the obj.member syntax. If you specify a pointer of type ArrayList<T>::ArrayList(ArrayList<T>* list) , you will need to use the list->member or (*list).member , not to mention that it is checked first if there is list 0 / nullptr (you cannot dereference null pointers).

You mix syntax for pointers and syntax for values ​​/ references. convert all your list->x to list.x since you are not passing the list argument with pointers.

See this for different walkthroughs: http://ideone.com/3c5mJ

+3
source

"const" is used because it is a reference to some object that you will not modify. the link is used because it is the copy constructor and the default syntax, suppose the link.

But the most important thing: you can define and use this constructor in the same way as the one you are trying to write.

+2
source

All Articles