Jump to content
  • Advertisement
Sign in to follow this  
Falken42

order of destruction of objects on the stack

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, Are objects declared on the stack *guaranteed* to be destroyed in the reverse order that they are declared? For example, if I have the following classes:
class Foo {
public:
    void func();
};

class Bar {
private:
    Foo *foo;

public:
    Bar(Foo *f) : foo(f) { }
    ~Bar() { foo->func(); }
};
And the following function:
void hoge()
{
    Foo f;
    Bar b(&f);
}
Is this dangerous? Is there any possibility that 'f' will be destroyed before 'b'?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by bpoint
Are objects declared on the stack *guaranteed* to be destroyed in the reverse order that they are declared?


Yes. Also, class member variables and base class subobjects are guaranteed to be destroyed in the reverse order that they were created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!