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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:29:41+00:00 2026-06-13T04:29:41+00:00

I have the following sample code (a stripped down version of my programme) Class

  • 0

I have the following sample code (a stripped down version of my programme)

Class ‘some_class’ has a constructor with default parameters. The compiler is able to recognise this constructor as a copy constructor. In the main function, this constructor is called when I order for a copy constructed object called ‘b’. But when I construct ‘c’ from a function result, the compiler calls a compiler generated copy constructor (which copies the bit pattern). I can tell by the value of c.some_data, which should have been set by my own copy constructor to a value of 2.

1) What does the standard say about this?
2) Is my compiler broken?

I use MinGW with no options but a specification of my source file name and a name for the executable. I got my port of the gnu open source compiler from the official MinGW website, I’m using the latest version. Have I found a bug, or is this due to my (mis)understanding of c++?

Thanks in advance

#include <iostream>
#include <string>

class some_class
{
public:
    some_class(int p = 0) :
        some_data(p)
    {
        std::cout << "user defined constructor (p = " << p << ")" << std::endl;
    }
    some_class(const some_class &, int = 0)
    {
        std::cout << "user defined copy constructor" << std::endl;

        some_data = 2;
    }

    int some_data;
};

extern some_class some_function_returning_some_class_object();

int main(int, char **)
{
        std::cout << "creating a, with no parameters" << std::endl;
    some_class a;
        std::cout << "creating b, a copy of a" << std::endl;
    some_class b = a;
        std::cout << "creating c, copy constructed from a function result" << std::endl;
    some_class c = some_function_returning_some_class_object();
        std::cout << "c.some_data = " << c.some_data << std::endl;
}

some_class some_function_returning_some_class_object()
{
    some_class a(1);

    return a;
}

The output is as follows:

creating a, with no parameters
user defined constructor (p = 0)
creating b, a copy of a
user defined copy constructor
creating c, copy constructed from a function result
user defined constructor (p = 1)
c.some_data = 1
  • 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-13T04:29:42+00:00Added an answer on June 13, 2026 at 4:29 am

    The compiler is not using the compiler-defined default copy constructor. It is presumably using return value optimization to skip the copy altogether.

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

Sidebar

Related Questions

I have the following sample code. class bar is derived from the base class
I have the following sample piece of code, public class test implements Runnable{ public
Lets say we have following sample code in C#: class BaseClass { public virtual
I have the following sample code and noticed that if I attempt to use
I have the following sample code which doesn't seem to want to run. import
I have the following code sample that im trying to wrap my head around
I have the following code sample: float val = 16777216.0F; var badResult = Convert.ToDecimal(val);
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I have downloaded sample code from Apple Center.I also have gone through following question:
I am new to Emacs, and I have the following code as a sample.

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.