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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:44:50+00:00 2026-05-13T20:44:50+00:00

I am reading effective C++ in Item 5, it mentioned two cases in which

  • 0

I am reading effective C++ in Item 5, it mentioned two cases in which I must define the copy assignment operator myself. The case is a class which contain const and reference members.

I am writing to ask what’s the general rule or case in which I must define my own copy constructor and assignment operator?

I would also like to know when I must define my own constructor and destructor.

Thanks so much!

  • 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-13T20:44:51+00:00Added an answer on May 13, 2026 at 8:44 pm

    You must create your own copy constructor and assignment operator (and usually default constructor too) when:

    • You want your object to be copied or assigned, or put into a standard container such as vector
    • The default copy constructor and assignment operator will not do the Right Thing.

    Consider the following code:

    class A; // defined elsewhere
    class B {
    private:
        A *my_very_own_a;
    };
    

    If you let the automatic copy constructor copy a B, it will copy the A * pointer value across, so that the copy points to the same A instance as the original. But part of the design of this class is that each B has its own A, so the automatic copy constructor has broken that contract. So you need to write your own copy constructor which will create a new A for the new B to point to.

    However, consider this case:

    class A; // defined elsewhere
    class B {
    private:
        A *shared_reference_to_a;
    };
    

    Here each B contains a pointer to an A, but the class contract doesn’t demand a unique A for each B. So the automatic copy constructor might well do the Right Thing here.

    Note that both examples are the same code, but with different design intent.

    An example of the first situation might be B == dialog box, A == button. If you create a copy of a dialog box, it probably needs a copy of all the contents too.

    An example of the second might be B == dialog box, A == reference to window manager. If you copy a dialog box, the copy probably exists in the same window manager as the original.

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

Sidebar

Related Questions

I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate
I've been reading Josh Bloch's 'Effective Java 2nd Edition'. Item 43 states 'Return empty
I am reading Bill Wagner's book Effective C# . In Item 32 he is
I just started reading Effective C++ today and got to the point where the
recently I've been reading through Scott Meyers's excellent Effective C++ book. In one of
I am currently reading Effective C# and found the following line very confusing: Last,
Reading this question I found this as (note the quotation marks) code to solve
Reading an article called Increase LINQ Query Performance in July's MSDN magazine, the author
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through the Flickr API documentation it keeps stating I require an API key

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.