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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:36:52+00:00 2026-05-28T01:36:52+00:00

Why aren’t copy constructors chained (like default ctors or dtors) so that before the

  • 0

Why aren’t copy constructors chained (like default ctors or dtors) so that before the derived class’s copy constructor is called, the base class’s copy constructor is called? With default constructors and destructors, they are called in a chain from base-to-derived and derived-to-base, respectively. Why isn’t this the case for copy constructors? For example, this code:

class Base {
public:
    Base() : basedata(rand()) { }

    Base(const Base& src) : basedata(src.basedata) {
        cout << "Base::Base(const Base&)" << endl;
    }

    void printdata() {
        cout << basedata << endl;
    }

private:
    int basedata;
};

class Derived : public Base {
public:
    Derived() { }

    Derived(const Derived& d) {
        cout << "Derived::Derived(const Derived&)" << endl;
    }
};


srand(time(0));


Derived d1;      // basedata is initialised to rand() thanks to Base::Base()

d1.printdata();  // prints the random number

Derived d2 = d1; // basedata is initialised to rand() again from Base::Base()
                 // Derived::Derived(const Derived&) is called but not
                 // Base::Base(const Base&)

d2.printdata();  // prints a different random number

The copy constructor doesn’t (can’t) really make a copy of the object because Derived::Derived(const Derived&) can’t access basedata to change it.

Is there something fundamental I’m missing about copy constructors so that my mental model is incorrect, or is there some arcane (or not arcane) reason for this design?

  • 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-28T01:36:52+00:00Added an answer on May 28, 2026 at 1:36 am

    The copy constructor doesn’t (can’t) really make a copy of the object because Derived::Derived(const Derived&) can’t access pdata to change it.

    Sure it can:

    Derived(const Derived& d)
        : Base(d)
    {
        cout << "Derived::Derived(const B&)" << endl;
    }
    

    If you don’t specify a base class constructor in the initializer list, its default constructor is called. If you want a constructor other than the default constructor to be called, you must specify which constructor (and with which arguments) you want to call.

    As for why this is the case: why should a copy constructor be any different from any other constructor? As an example of a practical problem:

    struct Base
    {
        Base() { }
        Base(Base volatile&) { } // (1)
        Base(Base const&)    { } // (2)
    };
    
    struct Derived : Base
    {
        Derived(Derived&) { }
    };
    

    Which of the Base copy constructors would you expect the Derived copy constructor to call?

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

Sidebar

Related Questions

Why aren't final variables default initialized? Shouldn't the default constructor initialize them to default
Aren't Javascript blocks supposed to execute in the order that they are placed on
For various reasons that aren't too germane to the question, I've got a table
Can't figure out why my tags aren't showing in the form that is used
I see that the NSLocale bindings aren't complete in MonoTouch so I am having
Why aren't they const? I believe that's flawed API design. Or am I missing
My expression aren't great and I would like to improve on them so I
It seems that spinlocks aren't that great as they waste CPU cycles when the
I know that key-value pairs aren't good database design, aren't normalized etc however in
I have a situation where my language contains some words that aren't known at

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.