Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:40:16+00:00 2026-05-31T18:40:16+00:00

Take these two classes for instance: struct Owned { Owned() : i() { }

  • 0

Take these two classes for instance:

struct Owned {
    Owned() : i() { }

    void print() { cout << ++i << endl; }

    int i;
};

struct Owner {
    Owner(Owned& o) : o(o) { }

    Owned& o;

    ~Owner() { o.print(); }
};

Is it dangerous to use them in this way?

int main() {
    Owned owned;
    Owner owner(owned);
}

It seems that, depending on the order in which they are destructed, that this could cause the destructor of owner to call a function on a destroyed owned. Is the order of destruction of local variables defined, and how can you make a situation where two instances refer to each other work?

Forgive me if this is common knowledge, I haven’t read anything about it anywhere though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:40:18+00:00Added an answer on May 31, 2026 at 6:40 pm

    Local variables are destroyed in the opposite order of being created. In your case, you’re fine, since owner will always be destroyed before owned.

    §6.6 [stmt.jump] p2

    On exit from a scope (however accomplished), objects with automatic storage duration (3.7.3) that have been constructed in that scope are destroyed in the reverse order of their construction.

    However, care has to be taken if you can reassign the owned member after construction.

    how can you make a situation where two instances refer to each other work?

    Don’t have them access each other in their destructor. Or make clear who exactly gets destroyed first, maybe with a callback or flag that gets passed. Example:

    struct two;
    struct one{
      two* other;
    
      one(two* o = nullptr) : other(o) {}
      ~one(){ if(other) other.other = nullptr; }
    };
    
    struct two{
      one* other;
      two(one* o = nullptr) : other(o) {}
      ~one(){ if(other) other.other = nullptr; } 
    };
    

    This will ensure that both objects never reference a non-existent one.

    Mutually referencing objects are very rare anyways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take these two Java classes: class User { final Inventory inventory; User (Inventory inv)
Take these two classes for example (C++) class B1 { public: }; class B2
I have to classes, an Executer with these methods: Executer() struct Execute(string s) Lookup(string
take two following classes: class Test1{ public: Test1()=default; Test1(char in1,char in2):char1(in1),char2(in2){} char char1; char
I have two classes, namely User & UserProfile User contains(few attributes like):- userId loginId
Take two base classes A and B which are similar but are preferred to
I am getting a little confused and need some help please. Take these two
I have two classes public class News { public virtual int Id { get;
Is there any way I can take two WinForms elements of the same size/type,
I could do this using loops, but is there a way to take two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.