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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:46:39+00:00 2026-06-15T04:46:39+00:00

Possible Duplicate: What are copy elision and return value optimization? I have the following

  • 0

Possible Duplicate:
What are copy elision and return value optimization?

I have the following program:

#include <iostream>

using namespace std;

class Pointt {
public:
    int x;
    int y;

    Pointt() {
        x = 0;
        y = 0;
        cout << "def constructor called" << endl;
    }

    Pointt(int x, int y) {
        this->x = x;
        this->y = y;
        cout << "constructor called" << endl;
    }

    Pointt(const Pointt& p) {
        this->x = p.x;
        this->y = p.y;
        cout << "copy const called" << endl;
    }

    Pointt& operator=(const Pointt& p) {
        this->x = p.x;
        this->y = p.y;
        cout << "op= called" << endl;
        return *this;
    }
};

Pointt func() {
    cout << "func: 1" << endl;
    Pointt p(1,2);
    cout << "func: 2" << endl;
    return p;
}


int main() {
    cout << "main:1" << endl;
    Pointt k = func();
    cout << "main:2" << endl;
    cout << k.x << " " << k.y << endl;
    return 0;
}

The output I expect is the following:

main:1
func: 1
constructor called
func: 2
copy const called
op= called
main:2
1 2

But I get the following:

main:1
func: 1
constructor called
func: 2
main:2
1 2

The question is: why doesn’t returning an object from func to main call my copy constructor?

  • 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-15T04:46:40+00:00Added an answer on June 15, 2026 at 4:46 am

    This is due to Return Value Optimization. This is one of the few instances where C++ is allowed to change program behavior for an optimization.

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

Sidebar

Related Questions

Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Possible Duplicate: How does Copy-local work? I have following situation: there's a project named
Possible Duplicate: NSString @property, using copy instead of retain I have a basic memory
Possible Duplicate: copy data from one matrix to another I have an array in
Possible Duplicate: deep copy NSMutableArray in Objective-C ? I have two arrays of length
Possible Duplicate: How disable Copy, Cut, Select, Select All in UITextView I have some
Possible Duplicate: question about copy constructor if I have this snippet of the code
Possible Duplicate: how to copy char * into a string and vice-versa I have
Possible Duplicate: Why copy constructor is not called in this case? Considering the following
Possible Duplicate: Copy column from one Excel sheet to another Excel sheet I have

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.