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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:07:42+00:00 2026-06-16T03:07:42+00:00

If I have a function like the following: stack fillStack(){ stack a; return a;

  • 0

If I have a function like the following:

stack fillStack(){
  stack a;
  return a;
}

void main(){
  stack s=fillStack();
}

Consider we have a class called stack.
How many constructor and destructor will be called?

  • 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-16T03:07:44+00:00Added an answer on June 16, 2026 at 3:07 am

    Here is what should be happening:

    stack fillStack() {
      stack a;  // constructor
      return a; // copy constructor and destructor a
    }
    
    int main(){
      stack s=fillStack(); // copy constructor and destructor of the temporary
    } // destructor s
    

    In practice the standard explicitly allows the copy-constructors to be
    optimized away (this is called copy elision) and the value to be
    constructed in place. That could end up looking something like this:

    void fillStack(stack&);
    
    int main() {
      stack s;
      fillStack(s); // with reference
    }
    

    Although, copy-construction must still be well-formed even if the
    compiler applies this transformation. If copy-construction can have
    side-effects this optimization can lead to somewhat odd behavior (try
    printing something from the copy-constructor and observe the behavior
    on different optimization levels).

    This optimization becomes largely unnecessary with C++11 due to
    move-semantics.

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

Sidebar

Related Questions

I have a function like the following: // Convert To JPG // public string
i have a variable like the following and i want a function to only
I have the following function that takes a number like 5 and creates a
I have a function like this void doSmth(Long... paramg){ } But I can't pass
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
Suppose I have the following function, the value it produces is needed by many
I have the following class: #include <string> #include <stack> #include <queue> #include map.h using
Say I have the following; public function onBellyPatch_Two(e:MouseEvent):void { inBelly_Two.visible = true; } inBelly_Two
I have the following Oracle function: function get_job_no return number is V_job_no number; begin
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =

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.