Important: Please read the Qt Code of Conduct -

A pointer to a class that contains pointers

  • hi i have a class that contains pointers,the class iherits nothing
    @class MyClass

    //i have pointers here

    now i have to use this class as a pointer in vector like this:
    @QVector<MyClass*> classes;
    //push some classes in here but
    //when i remove an element
    //the destructor doesn't get called,and i think that is the true definition of memory leak@

    so how do i make it call the destructor

  • remove only removes the pointer from the vector, but does not delete the object itself, so you need to delete the object when removing it.

    @MyClass *;
    delete class;@

  • If you don't want to explicitly call delete, you could use a vector of shared pointers instead of using raw pointers in the vector.
    Unless you use said shared pointer you should implement the assignment operator and copy-constructor.

    @MyClass instance1;
    MyClass instance2 = instance1;
    // at this point the private pointers in instance1 have been deleted.

Log in to reply