0

I wanted to ask for information: I was asked to pass parameters by value while, in case we are in a situation of the type

 List <ExpensiveObject> list;
 list.insert (expensive_object);

I don't want to pass the parameter by value.

string line;
ifstream myfile (R"(../tests/ListInteger.csv)");
if (myfile.is_open())
{
    while (getline (myfile,line))
    {
        char c[1000];
        strcpy(c, line.c_str());
        List::List<int> list;
        List::List<int*> listTwo;

        for (int i= 0; i<line.length(); i++) {
            int c48=c[i]-48;
            list.insert(c48);
        }

        for (int i= 0; i<line.length(); i++) {
            int c48=c[i]-48;
            listTwo.insert(reinterpret_cast<int *>(c48));
        }
        list.operator+(reinterpret_cast<List::List<int> *>(&listTwo));
        REQUIRE(listTwo.size() == list.size()*2);

could this management be correct? How can I handle this?

here you will find the function that deals with the insertion:

template <class T> typename List<T>::Node *List<T>::insert(T key) {
  if (head == 0) {
    head = new List<T>::Node(key);
    this->head = head;
    List<T>::current = this->head;
    listSize++;
    return this->head;
  } else {
    List<T>::Node *tempOne = new List<T>::Node(key);
    List<T>::current->setnext(tempOne);
    List<T>::current = List<T>::current->getnext();
    listSize++;
    return this->head;
  }
}
Omar05
  • 1
  • 4
  • https://stackoverflow.com/questions/40185665/performance-cost-of-passing-by-value-vs-by-reference-or-by-pointer – πάντα ῥεῖ Feb 28 '21 at 10:25
  • Why the `list.operator+()` instead of `list += ...`, and the `reinterpret_cast *>()` which seems totally unnecessary? Also, inside `List::insert` you don't have to repeat `List`, and `this->head` seems wrong to, just write `head` (I don't see any local variable declared named `head`). – G. Sliepen Feb 28 '21 at 10:35
  • Casting a `List` to a `List ` will most likely lead to undefined behaviour. `listTwo.insert(reinterpret_cast(c48));` is also suspect. Interpret an `int` as `int *`? I fail to see what this is supposed to do. – Lukas-T Feb 28 '21 at 10:52
  • In general I don't understand what you are trying to do. By convetion `operator+` will return a _new_ instance of `List` that contains the data from lhs and rhs _without_ modifying any of it's operands. But you discard the result, which hints that `List::operator+` isn't implemented properly or should be `operator+=` instead. – Lukas-T Feb 28 '21 at 11:03

0 Answers0