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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:02:38+00:00 2026-05-16T01:02:38+00:00

I tried to use copy constructor using statement: X y = X(); But copy

  • 0

I tried to use copy constructor using statement:

X y = X();

But copy constructor is not being called. I am using g++ 4.1.0. I set both X(const X&) and X(x&) constructor in the class.

Is this supposed to work or I am doing some very basic problem in the code?

My code for the class is

class A
{
 public:
  int i;

  A(int ii)
  {
   i = ii;
  }

  A(const A&)
  {
   i = 5;
  }

  A(A&)
  {
   i = -1;
  }

  A()
  {
   i = 5000;
  }
};

When I use it using
A a = A(); or A a = A(100);, it does not work but when i use it A a(b); or A a = b; it works fine.

What is the point I am missing? I saw that according to wikipedia , it should work but it’s not working in my case :(.

Thanks in advance for all your answers and comments.

  • 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-05-16T01:02:38+00:00Added an answer on May 16, 2026 at 1:02 am

    The compiler is permitted to elide the call to the copy constructor in certain situations. Initializing an object from a temporary is one of them. In this case, the temporary is simply constructed in-place instead of constructing a temporary and then copying it into the named object.

    You can call the copy constructor by constructing a named object then making a copy of that:

    X x;
    X y = x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Both @"some string" (apparently not, read @BoltClock's second comment) and… May 16, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer You will need to keep a list or 'dictionary' of… May 16, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer What you want is an IBOutletCollection. That allows you to… May 16, 2026 at 4:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have tried using the following Perl code, but the worksheet does not copy
I'm trying to use FindControl() to SET the values of some DropDownLists on a
Problem: When dealing with web-applications that require grid-based input (but is not a fullblown
in my project, I let users pick pictures using the FileReference class. I then
I have two indexed (8-bits) Bitamp, both using the same Palette in C# (.NET
Ive tried the gtk method, but it is very slow and doesn't work for
I particularly like the simplicity of using STL containers in the straightforward way. I
I'm trying to use RAII concepts with an STL container of ofstream objects. For
I'm writing an app for WM that handles incoming SMS events. I tried making
The background I use VS2010, DataGrid (the one delivered with WPF) and I manually

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.