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 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

Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
E,g class Test { public: void setVal(const std::string& str) { this.isVal = str; //This
Given a declaration like this: class A { public: void Foo() const; }; What
class test { public: test &operator=(const test & other){} // 1 const test &
class a { protected: const int _ID; public: a::a(int id){}; a::top(int num); }; class
class String { private: char* rep; public: String (const char*); void toUpper() const; };
What are the cons of some code like this: public class Class1 { public
This is my action class public Iterator<CgConsultant> getSearchresult() { List<CgConsultant> list =userSearch.getConsultantMatches(searchstring); System.out.println(The size
i have a problem with this class. the goal is to make the main
this is my generic class: template<class T, class PrnT> class PersonalVec { public: PersonalVec();

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.