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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:43:26+00:00 2026-05-13T06:43:26+00:00

In writing a copy constructor for one of my classes ( which holds a

  • 0

In writing a copy constructor for one of my classes ( which holds a few objects of other UDTs ), I am required to create a default constructor for those UDTs, even though they were never really meant to have one.

Is it fine to just implement a blank default constructor and be done with it? The only time the default constructor is invoked is during this copying, when the object is created and then the values of the corresponding object are copied to it. Thus, whatever values are assigned to the object in the default constructor will never actually be used.

The problem I see is that some member variables aren’t initialized in a blank default constructor. Should I just write one that gives dummy values instead? Any other recommended ways to handle this?

Edit: I understand that a copy constructor doesn’t NEED a default constructor if I were to define copy constructors for the other classes, but I didn’t, so it does need it.

  • 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-13T06:43:27+00:00Added an answer on May 13, 2026 at 6:43 am

    If you use an initializer list in the copy constructor, you don’t need a default constructor:

    #include <iostream>
    using namespace std;
    class Foo {
      Foo();            /* no default constructor */
    public:
      Foo(int i)        { cout << "Foo constructor (int)"  << endl; }
      Foo(const Foo& f) { cout << "Foo constructor (copy)" << endl; }
    };
    class Bar {
      Foo f;
    public:
      Bar()             : f(1)   { cout << "Bar constructor (default)" << endl; }
      Bar(const Bar& b) : f(b.f) { cout << "Bar constructor (copy)"    << endl; }
    };
    int main(void) {
      Bar b;
      Bar b_=b;
      return 0;
    }
    

    Results in:

    Foo constructor (int)
    Bar constructor (default)
    Foo constructor (copy)
    Bar constructor (copy)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In writing a copy constructor for a class that holds a pointer to dynamically
I am having problem in writing copy constructor for pointers to objects. This is
I'm writing an app that supposed to copy a bunch of files from one
I'm writing a lua script, and one of the things it does is copy
At this point, writing the copy constructor and assignment operator pair is well-defined; a
I am writing a script which must copy some names into a multidimensional array,
Like the old DOS way of creating a file by writing copy con foo.txt,
I am writing a test program to copy some file in Application data folder
I copy and paste code from this URL for creating and reading/writing a proc
I'm writing a simple ruby sandbox command-line utility to copy and unzip directories from

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.