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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:14:25+00:00 2026-05-13T00:14:25+00:00

I know that the compiler sometimes provides a default copy constructor if you don’t

  • 0

I know that the compiler sometimes provides a default copy constructor if you don’t implement yourself. I am confused about what exactly this constructor does. If I have a class that contains other objects, none of which have a declared copy constructor, what will the behavior be? For example, a class like this:

class Foo {
  Bar bar;
};

class Bar {
  int i;
  Baz baz;
};

class Baz {
  int j;
};

Now if I do this:

Foo f1;
Foo f2(f1);

What will the default copy constructor do? Will the compiler-generated copy constructor in Foo call the compiler-generated constructor in Bar to copy over bar, which will then call the compiler-generated copy constructor in Baz?

  • 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-13T00:14:26+00:00Added an answer on May 13, 2026 at 12:14 am
    Foo f1;
    Foo f2(f1);
    

    Yes this will do what you expect it to:
    The f2 copy constructor Foo::Foo(Foo const&) is called.
    This copy constructs its base class and then each member (recursively)

    If you define a class like this:

    class X: public Y
    {
        private:
            int     m_a;
            char*   m_b;
            Z       m_c;
    };
    

    The following methods will be defined by your compiler.

    • Constructor (default) (2 versions)
    • Constructor (Copy)
    • Destructor (default)
    • Assignment operator

    Constructor: Default:

    There are actually two default constructors.
    One is used for zero-initialization while the other is used for value-initialization. The used depends on whether you use () during initialization or not.

    // Zero-Initialization compiler generated constructor
    X::X()
        :Y()                // Calls the base constructor
                            //     If this is compiler generated use 
                            //     the `Zero-Initialization version'
        ,m_a(0)             // Default construction of basic PODS zeros them
        ,m_b(0)             // 
        m_c()               // Calls the default constructor of Z
                            //     If this is compiler generated use 
                            //     the `Zero-Initialization version'
    {
    }
    
    // Value-Initialization compiler generated constructor
    X::X()
        :Y()                // Calls the base constructor
                            //     If this is compiler generated use 
                            //     the `Value-Initialization version'
        //,m_a()            // Default construction of basic PODS does nothing
        //,m_b()            // The values are un-initialized.
        m_c()               // Calls the default constructor of Z
                            //     If this is compiler generated use 
                            //     the `Value-Initialization version'
    {
    }
    

    Notes: If the base class or any members do not have a valid visible default constructor then the default constructor can not be generated. This is not an error unless your code tries to use the default constructor (then only a compile time error).

    Constructor (Copy)

    X::X(X const& copy)
        :Y(copy)            // Calls the base copy constructor
        ,m_a(copy.m_a)      // Calls each members copy constructor
        ,m_b(copy.m_b)
        ,m_c(copy.m_c)
    {}
    

    Notes: If the base class or any members do not have a valid visible copy constructor then the copy constructor can not be generated. This is not an error unless your code tries to use the copy constructor (then only a compile time error).

    Assignment Operator

    X& operator=(X const& copy)
    {
        Y::operator=(copy); // Calls the base assignment operator
        m_a = copy.m_a;     // Calls each members assignment operator
        m_b = copy.m_b;
        m_c = copy.m_c;
    
        return *this;
    }
    

    Notes: If the base class or any members do not have a valid viable assignment operator then the assignment operator can not be generated. This is not an error unless your code tries to use the assignment operator (then only a compile time error).

    Destructor

    X::~X()
    {
                            // First runs the destructor code
    }
        // This is psudo code.
        // But the equiv of this code happens in every destructor
        m_c.~Z();           // Calls the destructor for each member
        // m_b              // PODs and pointers destructors do nothing
        // m_a          
        ~Y();               // Call the base class destructor
    
    • If any constructor (including copy) is declared then the default constructor is not implemented by the compiler.
    • If the copy constructor is declared then the compiler will not generate one.
    • If the assignment operator is declared then the compiler will not generate one.
    • If a destructor is declared the compiler will not generate one.

    Looking at your code the following copy constructors are generated:

    Foo::Foo(Foo const& copy)
        :bar(copy.bar)
    {}
    
    Bar::Bar(Bar const& copy)
        :i(copy.i)
        ,baz(copy.baz)
    {}
    
    Baz::Baz(Baz const& copy)
        :j(copy.j)
    {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To put it simply, instead of writing your classes in… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer I assume you mean the pseudo class :hover that you've… May 13, 2026 at 8:04 am
  • Editorial Team
    Editorial Team added an answer Section 3.4 of the Python Language Reference covers the magic… May 13, 2026 at 8:04 am

Related Questions

I know that the compiler sometimes provides a default copy constructor if you don't
When I compile something like this: public class MyClass { void myMethod(String name, String
Sometimes, it is useful to hide a string from a binary (executable) file. For
I know that the compiler will sometimes initialize memory with certain patterns such as
The problem I'm running into is that as far as I know the delete

Trending Tags

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

Top Members

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.