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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:30:56+00:00 2026-05-30T00:30:56+00:00

C++ says that to create a copy constructor for a class that uses composition,

  • 0

C++ says that to create a copy constructor for a class that uses composition, the compiler recursively calls the copy constructors for all the member objects. I tried that same thing in the below code:

class A
{
public:
    A(){cout<<"A constructor called"<<endl;}
    A(const A&){cout<<"A copy constructor called"<<endl;}
};

class B
{
public:
    B(){cout<<"B constructor called"<<endl;}
    B(const B&){cout<<"B copy constructor called"<<endl;}
};

class C
{
    A a;
    B b;
public:
    C(){cout<<"C constructor called"<<endl;}
    C(const C&){cout<<"C copy constructor called"<<endl;}// If you comment this line, you will get output: Case 1 (see below)     and if you don't comment, you will get o/p: case 2(see below)
};


void main()
{
    C c;
    cout<<endl;
    C c2 = c;
}`

Case 1:

A constructor called

B constructor called

C constructor called

A copy constructor called

B copy constructor called

Case 2:

A constructor called

B constructor called

C constructor called

A constructor called

B constructor called

C copy constructor called

My doubt is that the o/p for case 2 should be: A, B, C, constructor called and then.. A, B, C copy constructor called. But it is not happening. Please help.

  • 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-30T00:30:57+00:00Added an answer on May 30, 2026 at 12:30 am

    That would happen, except that you have provided your own copy constructor for C, which tells the compiler “don’t provide the default copy constructor.”

    If you want your copy constructor to do the same thing that the implicitly defined copy constructor would do (along with your extra printing), you’d need to define it as follows:

    C(const C& other)
        : a(other.a), b(other.b)
    {
        std::cout << "C copy constructor called" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says. How would I create an instance of a class that
The answer to this SO question says that you can create an api by
The MSDN site says: From your view class's function that handles the WM_CREATE message
So, just like the title says, I need to create an application that gets
MSDN says that you should use structs when you need lightweight objects. Are there
The question says it all really. Am I allowed derive a class from a
Here is the thing, I want to create a simply app that copy many
I am trying to create a program to copy all the files from one
The Qt documentation for QThread says to create a class from QThread, and to
MSDN says that public static members of System.Windows.Application are thread safe. But when I

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.