[try Beta version]
Not logged in

 
Deleting objects in template class

Oct 17, 2020 at 1:17pm
i've got my template class

template <typename T>
class List
and there's a method
1
2
3
4
  void clear_list(bool flag) {
                if(flag && std::is_pointer<T>::value == 1) delete current->data;
            }
        }

checking if T value is pointer then delete pointer, otherwise skip this line in fact the data is not on the heap but on stack created without new keyword
but it won't even compile throwing error
error: type 'class Student' argument given to 'delete', expected pointer|
but I tried to check if is it a pointer inside method

how can i create method which can delete T if it's pointer but otherwise skip it?
Oct 17, 2020 at 1:41pm
clear_list() is within class List, but your error is referring to class Student??
Oct 17, 2020 at 1:49pm
List<Student> *students = new List<Student>();

/* add random students using Student(int, int) */

students->display_list();
students->clear_list(false);
error points to this line in method when i call students->clear_list(false)


but it works well when i use objects on heap using new operator like this

List<Student*> *students = new List<Student*>();

/* add random students using new Student(int, int) */
students->display_list();
students->clear_list(false);
there's no problem
Last edited on Oct 17, 2020 at 2:01pm
Topic archived. No new replies allowed.