class a
{
public:
vector<double> * vec;
void memfunc();
}
void a::memfunc()
{
vec = new vector<double> ;
vec->push_back(1);
}
main()
{
a *object = new a();
a.memfunc();
}
and now if i delete object ? vec will be deleted ?? or first i need to delete vec and then object ??? please help me
class A {
public:
// called when the object is created: Initializes the vector
A() : vec(nullptr) {
vec = new vector<double>;
}
// called when deleting the object: Destroys the vector
~A() {
delete vec;
}
void memfunc() {
vec->push_back(1);
}
private:
vector<double>* vec; // I'm assuming you need it on the heap.
};
int main() {
// Constructs 'object', also constructs 'vec'
A* object = new A;
object->memfunc(); // works fine
delete object; // delete the object, also deleting 'vec';
return 0;
}