class B { ... } class A { private: vector<B *> data; public: A() { ... } A~() { //прохожу по каждому элементу data и вызываю для него delete; } void set(string str) { data.push_back(new B(str)); } } 

Now I need to create a getter that returns data. If you write this:

 vector<B *> get() const { return data; } 

All values ​​are copied. As a result, I will need to delete and copy the vector. It is necessary to write so that after calling the destructor A, all values ​​of the vector are removed, that is, the memory is completely freed.

    1 answer 1

    A link to a vector to return?

     vector<B *> &get() const { return data; } 
    • @nanotexnik, error C2440 cannot convert const to & - huxi
    • unnoticed when copying your code, remove const - nanotexnik
    • @nanotexnik, thanks. I put const on the machine for all getters, but in this case it was not necessary. - huxi