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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:33:44+00:00 2026-06-15T07:33:44+00:00

Possible Duplicate: What’s the motivation behind having copy and direct initialization behave differently? And

  • 0

Possible Duplicate:
What’s the motivation behind having copy and direct initialization behave differently?

And by copy initialization, I mean like so:

struct MyStruct
{
    MyStruct(int) {}
    MyStruct(const MyStruct&) {}
};

MyStruct s = 5; // needs *both* the int and copy constructor

Despite programming in C++ for years, I never realized the above code required the copy constructor (thanks to jogojapan). The temporary had always been elided, and as such I never even knew it even existed (at least on a superficial level, despite it being optimized away) until it was pointed out to me.

After a decent amount of googling, I get the idea of how it works. My question is why is it the way it is?

Why didn’t the standard make it so that the above example doesn’t need the copy constructor? Is there some specific case/example that shows that requiring the copy constructor in this type of initialization is important?

Without a decent explanation of why things are they way they are, I just see this as an annoying artifact, but I’d rather not be ignorant if there’s something important that I’m missing.

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

    Copy initialization of an object is ambiguous to direct initialization, both can be used to the same extent in order to set values equal to each other.

    int a = 4;
    int a = int(4);
    int a(4);
    

    all of these calls are ambiguous, they all set a equal to 4. The reason for a copy constructor in the case of an integer is convenience, imagine c++ data types without this

    int a(foo(b,r)); //a little messy for a variable declaration
    int a = foo(b,r) //ok, cleaner
    

    you also might might to use an implicit and explicit copy constructor, here is an example program that uses a copy constructor explicitly to handle imaginary numbers:

    #include <iostream>
    using std::cout;
    using std::endl;
    class complexNumbers {
      double real, img;
    public:
      complexNumbers() : real(0), img(0) { }
      complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
      explicit complexNumbers( double r, double i = 0.0) { real = r; img = i; }
      friend void display(complexNumbers cx);
    };
    void display(complexNumbers cx){
      cout<<&quot;Real Part: &quot;<<cx.real<<&quot; Imag Part: &quot;<<cx.img<<endl;
    }
    int main() {
      complexNumbers one(1);
      display(one);
      complexNumbers two =2;
      display(200);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# driver development? I would like to know if I can do
Possible Duplicate: Progressbar togther with asyncTask I am having a expandableListView in a project
Possible Duplicate: C++ convert int and string to char* Hello, i am making a
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: php == vs === operator Reference - What does this symbol mean
Possible Duplicate: Generating a Call Graph in R I'd like to systematically analyze a
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: What is printf’s behaviour when printing an int as float? int main()
Possible Duplicate: Reset AutoIncrement in SqlServer after Delete I'm having an annoying issue with

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.