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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:02:41+00:00 2026-05-11T21:02:41+00:00

class A { public: A(const int n_); A(const A& that_); A& operator=(const A& that_);

  • 0
class A
{
 public:
  A(const int n_);
  A(const A& that_);
  A& operator=(const A& that_);
};

A::A(const int n_)
{ cout << "A::A(int), n_=" << n_ << endl; }

A::A(const A& that_)    // This is line 21
{ cout << "A::A(const A&)" << endl; }

A& A::operator=(const A& that_)
{ cout << "A::operator=(const A&)" << endl; }

int foo(const A& a_)
{ return 20; }

int main()
{
  A a(foo(A(10)));    // This is line 38
  return 0;
}

Executing this code gives o/p:

A::A(int), n_=10
A::A(int), n_=20

Apparently the copy constructor is never called.

class A
{
 public:
  A(const int n_);
  A& operator=(const A& that_);
 private:
  A(const A& that_);
};

However, if we make it private, this compile error occurs:

Test.cpp: In function ‘int main()’:
Test.cpp:21: error: ‘A::A(const A&)’ is private
Test.cpp:38: error: within this context

Why does the compiler complain when it doesn’t actually use the copy constructor?
I am using gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)

  • 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-11T21:02:42+00:00Added an answer on May 11, 2026 at 9:02 pm

    Core defect 391 explains the issue.

    Basically, the current C++ standard requires a copy constructor to be available when passing a temporary of class type to a const reference.

    This requirement will be removed in C++0x.

    The logic behind requiring a copy constructor comes from this case:

    C f();
    const C& r = f(); // a copy is generated for r to refer to
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class test { public: int data; test(int var = 0):data(var){cout<<constructor<<endl;} ~test(){ cout<<destructor<<endl; } test(const
class Sample { public: Sample(){} Sample(const Sample& obj){ cout<<C.C. with 1 argument called<<endl;} Sample(const
Consider the belo code snippet: class Base { public: Base() { cout<<Constructor<<endl; } Base(const
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
class Base{ //... public: int get()const{ // const // Do something. } int get(int
Use case: class A { static int s_common; public: static int getCommon () const
$ cat Const.java public class Const { String Const(String hello) { return hello; }
Here is my code: class Soldier { public: Soldier(const string &name, const Gun &gun);
I have a class template <typename Iterator, typename Value> class Foo { public: Foo(const
Is this the only way of overloading the ostream& operator<< for a derived class

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.