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 334213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:02:00+00:00 2026-05-12T10:02:00+00:00

The following code gives a different number of destructors when compiled on GCC and

  • 0

The following code gives a different number of destructors when compiled on GCC and vc9. AFAIK when run on vc9 i get 5 destructors showing, which I understand. The + overloaded operator is called, and two object are created, when returned a temporary object is created. This makes destruction of 3 objects possible. When the overloaded = operator is called, one object is created and again a temporary one when returned. This sums it up to five destructs, not counting the three objects created at the start of main.

But when I compile on GCC I get 3.

Which leads me to guess that there isn’t a temporary object created when the function is terminated and returned ? or a question about different behavior between compilers. I simply do not know, and some clarification would be nice.

#include <iostream>
using namespace std;

class planetCord {
    double x, y, z;
public:
    planetCord() { x = y = z = 0; }
    planetCord(double j, double i, double k) { x = j; y = i; z = k; }    
    ~planetCord() { cout << "destructing\n"; }
    planetCord operator+(planetCord obj);
    planetCord operator=(planetCord obj);
    void show();
};

planetCord planetCord::operator +(planetCord obj) {
    planetCord temp;
    temp.x = x + obj.x;
    temp.y = y + obj.y;
    temp.z = z + obj.z;
    return temp;
}

planetCord planetCord::operator =(planetCord obj) {
    x = obj.x;
    y = obj.y;
    z = obj.z;
    return *this;
}

void planetCord::show() {
    cout << "x cordinates: " << x << "\n";
    cout << "y cordinates: " << y << "\n";
    cout << "z cordinates: " << z << "\n\n";
}

int main() {
    planetCord jupiter(10, 20, 30);
    planetCord saturn(50, 100, 200);
    planetCord somewhereDark;

    jupiter.show();
    saturn.show();
    somewhereDark.show();
    somewhereDark = jupiter + saturn;  
    jupiter.show();
    saturn.show();
    somewhereDark.show();
    return 0;
}
  • 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-12T10:02:00+00:00Added an answer on May 12, 2026 at 10:02 am

    GCC is implementing the “return value optimization” to skip temporaries. Set VC9 to Release mode and it’ll probably do the same.

    If GCC is really good, it is seeing that temp inside operator+ will be default-initialized, just like somewhereDark, and can just use a reference to somewhereDark directly if it tries to inline the function. Or it is seeing that the pass-by-value is useless and can instead pass-by-reference.

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

Sidebar

Related Questions

Given the following code why am I getting different values for a and b
The following code gives a NullReferenceException since XPathSelectElement can't navigate through the XPath expression
I'm having problems the following code gives me no results. however if I uncomment
when i am analyze my project following code gives me leakage warning. is there
The following C++ code gives an error while compiling: #include<iostream> using namespace std; class
The following code in VS2008 gives me a variable is already defined warning: if
the following code for some bizarre reason gives me a value for each row
The following code makes a list of names and 'numbers' and gives each person
Executing the below code gives me the following exception on the last line: InvalidOperationException:
The code below gives me the following error: incompatible types when assigning to type'char[10]'

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.