Skip to main content
gamedev.net
🔒 Locked

When is delete called?

Started by Aargyle Apr 24, 2003 at 10:28 AM 2 replies 200+ views
Original Post
Aargyle
Aargyle
I was curious how the scope destruction of objects interacts with constructors and destructors, and new and delete. The situation I am thinking of is this: class a { B* b; } a::a() { b = new B(); } a::~a() { delete b } Now, when the constructor is called memory is allocated, but doesn't this get destroyed when it goes out of scope? If not, does dynamically allocated memory ever get destroyed by leaving scope? And also, if scope doesn't call delete, will the OS *ever* call delete on anything without me specifically calling it? Thanks [edited by - aargyle on April 24, 2003 11:33:46 AM]
Qw3r7yU10p!
Qw3r7yU10p!

      
{ //beginning of scope

a test; //calls a::a() and creates an object of type 'a' called 'test'

} // end of scope a::~a() is called

// 'test' doesn't exist any more and is unknown to the compiler



when a::a() is called it calls new B(); which calls B::B()
when a::~a() is called is calls delete b; which calls B::~B()

[edited by - petewood on April 24, 2003 11:40:29 AM]
owl
owl
quote:
Original post by Aargyle
doesn''t this get destroyed when it goes out of scope?


nope. The scope of b is the a instance, not the a contructor.
[size="2"]I like the Walrus best.
Aargyle
Aargyle
Perfect thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.