Remy LeBeau
Remy LeBeau

Reputation: 65

C++: When deleting a dynamically allocated class, do private members that aren't on the heap get deleted as well?

For Example, let's say you have:

Class Test
{
     public:
     Test();
     private:
     int x = 0;
     int y = 3;
}

You declare an instance of Test using something like

Test * RRR = new Test();

Let's say somewhere down the line you type

delete RRR;

Does this also delete the memory that x and y took up?

Upvotes: 0

Views: 100

Answers (1)

Zac Wrangler
Zac Wrangler

Reputation: 1445

the short answer is yes, but strictly speaking, x and y are not deleted. The memory they tak will be released, because they are data members of the struct.

Even you don't define a destructor, the compiler will synthesized a default destructor for you, which will release the memory that the struct takes.

Upvotes: 1

Related Questions