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

  • SEARCH
  • Home
  • 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 6118163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:16+00:00 2026-05-23T15:24:16+00:00

When a variable is reassigned, the destructor is not called: Object foo = Object(a,b);

  • 0

When a variable is reassigned, the destructor is not called:

Object foo = Object(a,b);
foo = Object(c,d);

so, the destructor will only be called at the end of the scope for Object(c,d), which can obviously cause problems.
Now, in this specific case it doesn’t bother me too much: it is enough to declare 2 different objects:

Object foo1 = Object(a,b);
Object foo2 = Object(c,d);

In this way the destructor of both objects will be called at the end.

However, there is a case when I necessarily need to reassign a variable, i.e. in an object constructor such as:

SuperObject(Point point1, Point point2) : delay_object_(DelayObject(0)) {
  double distance = distance(point1, point2);
  double delay = distance / speed;

  delay_object_ = DelayObject(delay);
}

In fact the DelayObject parameter is not easy to calculate (in this example I also omitted a few other passages), and I want to avoid doing it in the initialisation list.

I thought I can force the deletion by putting the object in the heap and explicitly calling the destructor:

SuperObject(Point point1, Point point2) : p_delay_object_(new DelayObject(0)) {
  double distance = distance(point1, point2);
  double delay = distance / speed;

  delete p_delay_object_;
  p_delay_object_ = new DelayObject(delay);
}

but this really looks ugly to me, as I prefer to use dynamic allocation only when strictly necessary. Am I missing something?

Cheers!

  • 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-23T15:24:16+00:00Added an answer on May 23, 2026 at 3:24 pm

    “the destructor will only be called at the end of the scope for Object(c,d)”

    False. Object(c,d) is a temporary, and its destructor is called at the end of the full-expression which creates it. In this case, that’s the semi-colon at the end of foo = Object(c,d);. The destructor of foo is called at end-of-scope.

    The assignment operator of Object should free or re-use resources already held by foo, and copy resources held by the temporary. Not necessarily in that order (see copy-and-swap).

    Edit: in response to comment.

    Object foo = Object(a,b);
    

    Either

    1. A temporary is constructed using whatever two-argument constructor matches (a,b).
    2. foo is constructed using the copy-constructor, passing the temporary as argument.
    3. The temporary is destroyed.

    Or

    1. foo is constructed using whatever two-argument constructor matches (a,b).

    The implementation is free to do either – this is permitted by “copy constructor elision”.

    foo = Object(c,d);
    
    1. A temporary is constructed using whatever two-argument constructor matches (c,d).
    2. The assignment operator for class Object is called on foo, passing the temporary as argument.
    3. The temporary is destroyed.

    Some time later, at the end of the scope, foo is destroyed.

    In C++0x, move assignment would come into play if it exists for the class.

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

Sidebar

Related Questions

The variable filepath which is a string contains the value Música . I have
How can I unset variable? For example, PHP has an unset($var) function.
VARIABLE: ... UNARYOP: 'not' Expression; // unary operation BINARYOP: 'or' VARIABLE; Expression : (NIL
I've run into what appears to be a variable scope issue I haven't encountered
Say, I have some scope with variables, and a function called in this scope
A variable named $class contains the name of a class. How can I access
I am declaring a Skin class as a variable on my AppDelegate. I only
I see that $JAVA_HOME is a built-in variable in IzPack. Can I assign to
I can't tell if I'm misunderstanding the syntax or if I'm not understanding the
Variable variables seem pretty cool, but I can't think of a scenario where one

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.