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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:57:33+00:00 2026-05-23T00:57:33+00:00

consider the following: class X { public: X(int i) { cout << X(int i)

  • 0

consider the following:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

void main() {
    X x1(1);
    X x2 = X(1);
    X x3 = (X)1;
}

running this code produces this output:

X(int i)
X(int i)
X(int i)

I thought that all of the above three statements are equivalent as the copy CTOR is never called. However, changing X‘s copy CTOR to be private:

class X {
public:
    X(int i) { cout << "X(int i)" << endl; }
private:
    X(const X& x) { cout << "X(const X& x)" << endl; }
};

Will fail to compile (In visual studio 2010) with this error:

cannot access private member declared in class 'X'

So it seems the copy CTOR is involved somehow though I don’t quite understand how.

Thanks

  • 1 1 Answer
  • 3 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-23T00:57:34+00:00Added an answer on May 23, 2026 at 12:57 am
    X x1(1);
    X x2 = X(1);
    X x3 = (X)1;
    

    The reason is that all of these are not exactly equivalent.

    First one is direct-initialization, while the second and third is copy-initialization. For copy-initialization, copy-constructor must be public, or else compiler will give error.

    Now the question is, if 2nd and 3rd requires the copy-ctor to be public,then why the following output:

    X(int i)
    X(int i)
    X(int i)
    

    This surely says that copy-ctor is never called which is true. Compiler just elided the call to copy-ctor. According to §8.5/14, in such cases, the compiler is permitted to eliminate the need to call copy-constructor. That is why you don’t see copy-ctor being called.

    A little inside : in the 2nd and 3rd case, first a temporary is created by calling X(int i), then this temporary was supposed to be passed to the copy-ctor to copy-initialize the object being declared. But the compiler optimizes away this step, eliding the call to copy-ctor.

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

Sidebar

Related Questions

Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider the following : class A { public: int xx; A(const A& other) {
Consider the following code: class A { public: A& operator=( const A& ); const
Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Consider the following code : #include <vector> #include <iostream> class a { public: int
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following: class A { public: const int c; // must not be
Consider the following sample code: class Base { public: void f(); virtual void vf();

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.