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.