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

  • Home
  • SEARCH
  • 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 7557897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:15:10+00:00 2026-05-30T12:15:10+00:00

I am trying some new C++11 features on visual studio 11, started with the

  • 0

I am trying some new C++11 features on visual studio 11, started with the move constructor. I wrote a simple class called "MyClass" containing a move constructor:

class MyClass
{
public:
    explicit MyClass( int aiCount ) 
        : mpiSize( new int( aiCount ) ),
          miSize2( aiCount)
    {
    }

    MyClass( MyClass&& rcOther )
        : mpiSize( rcOther.mpiSize )
        , miSize2( *rcOther.mpiSize )
    {
       rcOther.mpiSize = 0;
       rcOther.miSize2 = 0;
    }

    ~MyClass() 
    {
        delete mpiSize;
    }

private:
    int *mpiSize;
    int miSize2;

};

I got there questions here:

  1. I assumed that the compiler would generate a move constructor for MyClass if I don’t implement one – but it doesn’t seems so?
  2. Is the implementation of the move constructor correct for MyClass?
  3. Is there a better way to implement the move constructor for MyClass?
  • 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-30T12:15:11+00:00Added an answer on May 30, 2026 at 12:15 pm
    1. MSVC++ implemented move constructors before the final version of the standard was out. In the version of the standard MSVC++’s implementation was based on, the rules for generating a default move constructor were ridiculously more strict than they are in the final version of the standard. See here: Why is this code trying to call the copy constructor? (specifically this answer and the comments on it) for more info on that. This has not been and will not be fixed in Visual Studio 11, for some unknown stupid reason because they had other priorities.

    2. No, you need to call std::move on the members of rcOther, and you initialise members with the corresponding members from the dying object (you misnamed miSize):

      MyClass( MyClass&& rcOther )
          : mpiSize( std::move(rcOther.mpiSize) )
          , miSize2( std::move(rcOther.miSize2) )
      {
         rcOther.mpiSize = 0;
      }
      

      It doesn’t make a difference for built in types like int and int*, but it definitely makes a difference for user-defined types.

      • The reason for this is that std::move just returns the argument casted into a T&&, an rvalue-reference, so that the correct constructor (the move constructor, T(T&&)) is called for each of the sub-objects. If you don’t use std::move on the members of the dying object, they will be treated like T&, and the copy constructor of your subobjects (T(T&)) will be called instead of the move constructor. That is very bad and thwarts almost the entire purpose of you having written a move constructor.

    3. You are doing some unnecessary things, like setting the integer to 0. You only need to set the pointer to 0 so that deleteing it won’t delete the resource of the new object you created.

    Also, if this is not a didactic exercise, you may want to consider using std::unique_ptr instead of managing the lifetime of your own object. This way you don’t even have to write a destructor for your class. Note that if you do that, using std::move to initialise the member from the dying member in the move constructor would be mandatory.


    • Konrad Rudolph in his answer has caught the fact that your class manages a non-automatic resource but does not follow the Rule of Five Three, Four, or Five. See his answer for more details on this.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started to implement some new html5 features (standards, nothing fancy) in my project.
I'm trying to use some of the new MFC feature pack controls on an
I'm trying to insert some new objects into a firebird database using NHibernate. I
I'm learning Silverlight and am trying to ingest some new concepts. Just so I
I am trying to some pretty basic RMI: // Context namingContext = new InitialContext();
I'm trying to put some specs around a new rails 3 project I am
I'm new to jQuery and i'm trying to write some code to go through
I'm trying to disassemble some of the DLL of the new WPF4 Beta2 framework.
I'm really new to ASP.NET MVC, and I'm trying to integrate some Javascript into
I am new to Haskell and trying to fiddle with some test cases I

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.