pointer deletions

i have class and one member variable

ex

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
This is what destructors are for: they perform takes upon the deletion of your object. Here is an example for your purposes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}
This is abuse of pointers and dynamic memory. There is no reason to use pointers or dynamic memory here.

As a general rule of thumb, if you can't use the default destructor, you're doing something wrong.
Topic archived. No new replies allowed.