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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:38:49+00:00 2026-05-16T20:38:49+00:00

If the operator= is properly defined, is it OK to use the following as

  • 0

If the operator= is properly defined, is it OK to use the following as copy constructor?

MyClass::MyClass(MyClass const &_copy)
{
    *this = _copy;
}
  • 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-16T20:38:49+00:00Added an answer on May 16, 2026 at 8:38 pm

    If all members of MyClass have a default constructor, yes.

    Note that usually it is the other way around:

    class MyClass
    {
    public:
        MyClass(MyClass const&);     // Implemented
        void swap(MyClass&) throw(); // Implemented
        MyClass& operator=(MyClass rhs) { rhs.swap(*this); return *this; }
    };
    

    We pass by value in operator= so that the copy constructor gets called. Note that everything is exception safe, since swap is guaranteed not to throw (you have to ensure this in your implementation).

    EDIT, as requested, about the call-by-value stuff: The operator= could be written as

    MyClass& MyClass::operator=(MyClass const& rhs)
    {
        MyClass tmp(rhs);
        tmp.swap(*this);
        return *this;
    }
    

    C++ students are usually told to pass class instances by reference because the copy constructor gets called if they are passed by value. In our case, we have to copy rhs anyway, so passing by value is fine.

    Thus, the operator= (first version, call by value) reads:

    • Make a copy of rhs (via the copy constructor, automatically called)
    • Swap its contents with *this
    • Return *this and let rhs (which contains the old value) be destroyed at method exit.

    Now, we have an extra bonus with this call-by-value. If the object being passed to operator= (or any function which gets its arguments by value) is a temporary object, the compiler can (and usually does) make no copy at all. This is called copy elision.

    Therefore, if rhs is temporary, no copy is made. We are left with:

    • Swap this and rhs contents
    • Destroy rhs

    So passing by value is in this case more efficient than passing by reference.

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

Sidebar

Related Questions

At this point, writing the copy constructor and assignment operator pair is well-defined; a
Help me please. object.cpp: bool OBJECT::operator== (const OBJECT &object) const { return *this ==
bool operator == (const MyString& left, const MyString& right) { if(left.value == right.value) return
I have been getting the following exception: The binary operator GreaterThanOrEqual is not defined
I have a class with a string property. I use the coalesce operator when
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
I have encountered the following code: class a { public: void * operator new(size_t
From this question I asked 5 minutes ago, it's clear that the following code
My code reviewers has pointed it out that the use of operator[] of the
In my Visual C++ program I use a custom operator new that uses malloc()

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.