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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:57:24+00:00 2026-06-12T09:57:24+00:00

Are the following assignment and copy move constructors the most efficient? if anybody have

  • 0

Are the following assignment and copy move constructors the most efficient?
if anybody have other way please tell me?
I mean what bout std::swap? and calling assignment through copy constructor is safe in the code below?

#include <iostream>
#include <functional>
#include <algorithm>
#include <utility>

using std::cout;
using std::cin;
using std::endl;
using std::bind;


class Widget
{

public:

    Widget(int length)
        :length_(length),
        data_(new int[length])
    {
        cout<<__FUNCTION__<<"("<<length<<")"<<endl;
    }

    ~Widget()
    {
        cout<<endl<<__FUNCTION__<<"()"<<endl;
        if (data_)
        {
            cout<<"deleting source"<<endl;
        } 
        else
        {
            cout<<"deleting Moved object"<<endl;
        }

        cout<<endl<<endl;
    }

    Widget(const Widget& other)
        :length_(other.length_),
        data_(new int[length_])
    {
        cout<<__FUNCTION__<<"(const Widget& other)"<<endl;
        std::copy(other.data_,other.data_ + length_,data_);
    }

    Widget(Widget&& other)
/*
        :length_(other.length_),
        data_(new int[length_])*/
    {
        cout<<__FUNCTION__<<"(Widget&& other)"<<endl;
        length_ = 0;
        data_ = nullptr;
        std::swap(length_,other.length_);
        std::swap(data_,other.data_);
    }

    Widget& operator = (Widget&& other)
    {
        cout<<__FUNCTION__<<"(Widget&& other)"<<endl;

        std::swap(length_,other.length_);
        std::swap(data_,other.data_);

        return *this;
    }

    Widget& operator = (const Widget& other)
    {
        cout<<__FUNCTION__<<"(const Widget& other)"<<endl;
        Widget tem(other);
        std::swap(length_,tem.length_);
        std::swap(data_,tem.data_);

        return *this;
    }
    int length()
    {
        return length_;
    }

private:

    int length_;
    int* data_;
};



int main()
{
    {
        Widget w1(1);
        Widget w2(std::move(Widget(2)));

        w1 = std::move(w2);
    }


    cout<<"ENTER"<<endl;
    cin.get();
    return 0;
}
  • 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-06-12T09:57:25+00:00Added an answer on June 12, 2026 at 9:57 am

    Looks fine from an efficiency POV, but contains an awful lot of duplicated code. I’d

    • Implement a swap() operator for your class.
    • Initialize length_ and data_ where they are declared.
    • Implement operations in terms of other operations whereever possible.

    You might want to use std::memcpy instead of std::copy since you’re dealing with a raw array anyway. Some compilers will do that for you, but probably not all of them…

    Here’s a de-duplicated version of your code. Note how there is only one place which needs to know how two instances of Widget are swapped. And only one place which knows how to allocate a Widget of a given size.

    Edit: You usually also want to use argument-dependent lookup to locate swap, just in case you ever have non-primitive members.

    Edit: Integrated @Philipp’s suggestion of making the assignment operator take it’s argument by value. That way, it acts as both move assignment and copy assignment operator. In the move case, not that if you pass a temporary, it won’t be copied, since the move constructor, not the copy constructor will be used to pass the argument.

    Edit: C++11 allows non-cost members to be called on rvalues for compatibility with previous versions of the standard. This allows weird code like Widget(...) = someWidget to compile. Making operator= require an lvalue for this by putting & after the declaration prevents that. Note though that the code is correct even without that restriction, but it nevertheless seems like a good idea, so I added it.

    Edit: As Guillaume Papin pointed out, the destructor should use delete[] instead of plain delete. The C++ standard mandates that memory allocated via new [] be deleted via delete [], i.e. it allows new' andnew []` to use different heaps.

    class Widget
    {
    public:
        Widget(int length)
            :length_(length)
            ,data_(new int[length])
        {}
    
        ~Widget()
        {
            delete[] data_;
        }
    
        Widget(const Widget& other)
            :Widget(other.length_)
        {
            std::copy(other.data_, other.data_ + length_, data_);
        }
    
        Widget(Widget&& other)
        {
            swap(*this, other);
        }
    
        Widget& operator= (Widget other) &
        {
            swap(*this, other);
            return *this;
        }
    
        int length() const
        {
            return length_;
        }
    
    private:
        friend void swap(Widget& a, Widget& b);
    
        int length_ = 0;
        int* data_ = nullptr;
    };
    
    void swap(Widget& a, Widget& b) {
        using std::swap;
        swap(a.length_, b.length_);
        swap(a.data_, b.data_);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following assignment: Implement a basic shopping basket without using any predefined
I have the following assignment: Write a complete 8086 program to perform the calculator
In my assignment i have a problem with reading a file. See the following
For a programming assignment, we have the following requirements: It needs to be a
I have defaulted my copy constructor and copy assignment operator as follows: Config(const Config&
i have following question. I tried assignment by value and by reference and as
Please look at the following code and tell me if it's going to cause
I have been learning about move constructors over the last day or so, trying
I got a segmentation fault when invoked a function immediately following a pointer assignment.
The following code is returning an expression unused warning on the assignment operation in

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.