Deleting objects in template class

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?
clear_list() is within class List, but your error is referring to class Student??
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
Registered users can post here. Sign in or register to post.