Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:09:54+00:00 2026-06-09T12:09:54+00:00

I’m working on a template Class in C++ similar to the ArrayList in java

  • 0

I’m working on a template Class in C++ similar to the ArrayList in java (yes I know vector does the same thing, this is not a utilitarian coding project).

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

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

Does this mean that the ArrayList has to be a constant? And why do I need the addressof operator?

I’m still learning the ropes of C++ so I’m a bit confused.

The prototypes are 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
The below code gets no errors, 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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:09:55+00:00Added an answer on June 9, 2026 at 12:09 pm

    Use

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

    as your constructor signature to pass the arraylist as a const reference. This is the proper signature for a copy constructor. Both the implementation and calling code needn’t change, unless you are modifying list within your ctor.

    When you do ArrayList<T>::ArrayList(ArrayList<T> list) you are creating a temporary copy of the entire ArrayList instance. (You mustn’t do this for ctors since it will invoke infinite recursion, as the new copy of list will use the same function definition and so on).

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

    By accepting a const reference in a function, you are saying that the function will not modify the contents of the reference (i.e. not do any modifying operations on it : it will be constrained to only const accesses). You should read about const, references and copy constructors before going further.

    Update:

    For pass by value or reference, you can access members via the obj.member syntax. If you were passing a pointer like ArrayList<T>::ArrayList(ArrayList<T>* list), you would have to use list->member syntax or (*list).member syntax, not to mention checking if list is 0/nullptr first (you can’t dereference null pointers).

    You are mixing syntax for pointers and syntax for value/references. convert all of your list->x into list.x since you aren’t passing the list argument using pointers.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.