Deleting an object

Good morning,
I have a task to create a certain amount of objects. However, an object has a variable called age. Once that variable reaches 10 it should be deleted. Is it possible? If it is how could i do that?
The code below creates a variable called age and deletes it once it reaches 10.

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
#include <iostream>

int main()
{
    int *pAge = new int;
    std::cout << "age created.\n\n";

    int &age = *pAge;
    age = 0;

    while (true)
    {
        std::cout << age << ' ' << std::flush;

        if (++age == 10)
        {
            delete pAge;
            std::cout << "\n\nage deleted.\n";

            break;
        }
    }

    return 0;
}

Note, the above frees the memory being used. Meaning other things are allowed to use that memory. But it will exist until something else starts using it.
Last edited on
Topic archived. No new replies allowed.