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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:16:58+00:00 2026-06-11T01:16:58+00:00

I was studying how a function returns an object by means of return-by-value. So,

  • 0

I was studying how a function returns an object by means of return-by-value. So, to test the theory, I ran a simple program that had a function returning an instance of myclass – a custom-made class.

#include <iostream>
#include <cstdio>
using namespace std;

class myclass {
    int i;
    public:
    void set_i(int n) { i=n; }
    ~myclass();
};

myclass f(int k); // return object of type myclass

int main()
{
    f(20);
    return 0;
}

myclass f(int k)
{
    myclass x;
    x.set_i(k);
    return x;
}

myclass::~myclass() {
cout << "hello\n";
}

I overloaded the destructor by placing a cout “hello” statement in it so that I would be able to track when the object’s local copy within the function as well as the temporary object created during execution of the return statement were destroyed.

So, I was expected 2 calls to the destructor; one for the function’s local copy of the object and one for the temporary object. But instead, I received only one!

Please share why my program did not output “hello” twice.

Thanks.

  • 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-06-11T01:17:00+00:00Added an answer on June 11, 2026 at 1:17 am

    That is an effect of return value optimization which eliminates the temporary object created to hold a function’s return value. This optimizes out the redundant copy constructor and destructor calls.

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

Sidebar

Related Questions

ok I have this code, that I'm studying class scope{ function printme(){ return hello;
I've been trying to do a function that returns the Cartesian Product of n
Studying Haskell, i'm trying to code a function takeIf that given a condition and
I was trying to use the function glMultiDrawElements while studying OpenGL (using red book)
I'm currently self-studying C for mastering an university project. In that project we have
I've been studying OpenCV tutorials and came across the assert function; what does it
I'm studying the linux-3.2.9 kernel, and in sched_rt.c function pick_next_highest_task_rt() there is a for
I'm studying for a test on Turing machines, and I'm stuck with a problem
Possible Duplicate: what is Object Cloning in php? I am studying a existing framework
I was studying a jquery easing function to use in a java application and

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.