-2

I need to define a copy constructor and an = operator overload for a class. Here's the code:

#include <iostream>
#define MAXITEM 100

using namespace std;

typedef float ItemType;

class List
{
public:
    List() {};  // default constrctor

    List(const List &x) { /* what to put here */ };  // copy constructor with deep copy

    bool IsThere(ItemType item) const {};  // return true or false to indicate if item is in the 
                                        // list

    void Insert(ItemType item) {};  // if item is not in the list, insert it into the list

    void Delete(ItemType item) {};  //  delete item from the list

    void Print() { // Print all the items in the list on screen
        for (int x = 0; x < length; x++)
            cout << info[x] << " ";
        cout << "\n";
    };  

    int Length() { return length; };   // return the number of items in the list

    ~List() {};  // destructor: programmer should be responsible to set the value of all the array elements to zero

    List & operator = (const List &x) { /* and here */ };  // overloading the equal sign operator

private:
    int length;
    ItemType  info[MAXITEM];

};

I tried just doing something like

info = x.info;

but it just gives me an "expression must be a modifiable lvalue" error.

braaterAfrikaaner
  • 1,072
  • 10
  • 20
syntience
  • 3
  • 5
  • 1
    Is this an excercise where you're asked to implement these or do you think you need to? If it's the latter then don't bother and just keep them defaulted. Also, looks like you want a `std::vector`. – Hatted Rooster Mar 02 '18 at 20:34
  • What are you trying to do here? Do you want a list? What's wrong with [`std::list`](http://en.cppreference.com/w/cpp/container/list)? – einpoklum Mar 02 '18 at 20:36
  • You can't assign arrays... you may however assign c++11's [`std::array`](http://en.cppreference.com/w/cpp/container/array) which would make your life easier (or as said before, `std::vector` if you want a dynamic size) – Caninonos Mar 02 '18 at 20:36
  • being asked to implement. i know this is basically asking for the answer, but i've looked around for a while (an hour or two?) for anything, and i can only find people describing what copy constructors and operator overloading are. copy constructors create an instance of an object by copying another object, and operator overloading... redefines what the operator does? i think? something like that. – syntience Mar 02 '18 at 20:37
  • No, don't. IF you absolutely have to write one then just use a vector for the info member and you can do `info = x.info;`. – Hatted Rooster Mar 02 '18 at 20:42
  • @einpoklum don't know what std::list is, and i'm supposed to write the function definitions for this unsorted list class. i'm sure there are easier ways to do all of this, and i know vectors are usually better than arrays, but i'm not trying to do anything practical here, just learning how to do copy constructors. the confusion is what that equal operator overload is for, and what code to put inside that and the copy ctor. – syntience Mar 02 '18 at 20:42
  • Copy constructors and operator= (assignment operator) are methods that are usually implicitly defined (the default implementation tries to copy construct or assign each member, if possible). But they can be explicitely defined (if that default implementation wouldn't work for instance). In that case, you can't assign an array to another array. However you can copy the array by manually copying each element, for instance with a for loop. – Caninonos Mar 02 '18 at 20:43
  • @SombreroChicken i don't know if i *can* use vectors though, since i didn't write any of this outside the function definition for Print(). the professor gave me the basic skeleton, and i need to write the definitions. – syntience Mar 02 '18 at 20:44
  • What a stupid exercise then. Okay, just copy them with a loop then. – Hatted Rooster Mar 02 '18 at 20:44
  • @Caninonos oh right, for loops exist. uh, how would i do that? or like, would the code for the copy ctor and the = operator be the same? or would i put the for loop in the = operator overload and just do info = x.info in the copy ctor or something? **edit:** i tried a simple for loop using info[y] = x[y], but i get a "no operator matches operands" error. – syntience Mar 02 '18 at 20:45
  • @syntience you won't be able to write `info = x.info`, as I said, `info` is an array and isn't assignable. However, you could indeed have `*this = the_argument_to_copy` in your copy constructor if you wanted to reuse your copy assignment operator. (note that other people suggested memcpy as well, this will work too as float is trivially copyable, and might be slightly more efficient depending on your compiler, remember not to memcpy things that aren't trivially copyable though) – Caninonos Mar 02 '18 at 20:50
  • @Caninonos yeah, i get the array stuff now. how would i format the for loop though? it's giving me an error by trying to info[counterVariable] = x.info[counterVariable]; **edit:** also, i would try memcpy, but we haven't learned that in class or anything, and i'm pretty sure my professor is expecting us to be able to do this using only what we've learned so far. – syntience Mar 02 '18 at 20:53
  • @syntience: So, follow the link to read about `std::list` - which is an (unsorted) list implementation that's part of the standard C++ library. But I guess that's not what you're after. – einpoklum Mar 02 '18 at 22:02
  • @einpoklum in retrospect, i could've taken a look to see how they did it, then gone off that – syntience Mar 04 '18 at 23:36
  • @syntience: Life lesson: Try not to reinvent the wheel (without very good reason). – einpoklum Mar 04 '18 at 23:59
  • @einpoklum understood. only reason i was doing this is because it was a required class assignment, but will heed your advice. – syntience Mar 05 '18 at 21:51

1 Answers1

3
List & operator = (const List &x)
{
    //first copy all the elements from one list to another
    for(int i = 0;i < MAXITEM;i++)
        info[i] = x.info[i];
    //then set the length to be the same
    length = x.length;
    return *this;
};

The above written code is a valid assignment operator in your case.

And the copy constructor is basically the same thing. You want to copy all the elements from another list (x) and then set the length to x.length, but you don't return dereferenced this pointer, because it's a copy constructor and it doesn't return anything.

MaikuZ
  • 56
  • 3
  • You need to set length as well. – Stephen Newell Mar 02 '18 at 20:53
  • oh, that works! i see, i absentmindedly tried to use length instead of MAXITEM for the for loop length. looks like that works for the = operator, but would i need any code in the copy constructor then? – syntience Mar 02 '18 at 20:57
  • You need to have code inside the brackets and the code is copy paste the same, only delete the return statement. You need to write the constructor, because you declared it. If you wouldn't have declared it the compiler would make a default copy constructor that would copy everything from one list to another. In that meaning both operator= and copy constructor are useless, because the default constructor would do the same (it copies byte by byte the values of all the variables from one object to another). – MaikuZ Mar 02 '18 at 21:03
  • 1
    Alternately write the copy constructor first and take advantage of the [Copy and Swap Idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) to implement `operator=`. – user4581301 Mar 02 '18 at 21:03
  • so the code for the copy ctor and the operator= are the same, except the copy ctor doesn't have a return since constructors and destructors don't have returns. >you need to write the constructor; you mean the copy or default? also, thanks for the help – syntience Mar 02 '18 at 21:11
  • @syntience yes the code is the same, except for the return. You need to write copy to make it work as you want to. If you leave it declared and with empty braces your copy constructor will do nothing. The default constructor could simply fill 0's in the info array and set the length to 0 and that is possibly expected from you. – MaikuZ Mar 02 '18 at 21:16