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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:27:22+00:00 2026-06-18T17:27:22+00:00

Consider this code: #include <iostream> #include <string> using namespace std; class Movable { public:

  • 0

Consider this code:

#include <iostream>
#include <string>

using namespace std;


class Movable {
public:
    Movable(const string& name) : m_name(name) { }
    Movable(const Movable& rhs) {
        cout << "Copy constructed from " << rhs.m_name << endl;
    }

    Movable(Movable&& rhs) {
        cout << "Move constructed from " << rhs.m_name << endl;
    }

    Movable& operator = (const Movable& rhs) {
        cout << "Copy assigned from " << rhs.m_name << endl;
    }

    Movable& operator = (Movable&& rhs) {
        cout << "Move assigned from " << rhs.m_name << endl;
    }

private:
    string m_name;
};


int main() {
    Movable obj1("obj1");
    Movable obj2(std::move(obj1));
    obj2 = std::move(obj1);     // For demostration only

    const Movable cObj("cObj");
    Movable tObj(std::move(cObj));
    tObj = std::move(cObj);     // For demonstration only
}

Its output is:

Move constructed from obj1
Move assigned from obj1
Copy constructed from cObj
Copy assigned from cObj

As you can see, in these lines,

Movable tObj(std::move(cObj));
tObj = std::move(cObj);     // For demonstration only

I intend to move cObj to tObj (the second move using the assignment operator is purely intended for demonstration). However, as you can see in the output, cObj is only copied to tObj.

The above example is only a demonstration and I do not know of any practical usage for this. But I will ask:

  1. Can I move a const object?
  2. If I can, is it safe to do it?

ADDITIONAL: I forgot to ask. If I can move a const object, how should I do it? (const_cast?)

  • 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-18T17:27:24+00:00Added an answer on June 18, 2026 at 5:27 pm

    Sure, as long as the object’s non-mutable state doesn’t change as a result of being moved from.

    Perhaps you have an object that corresponds to file content, or a database record. And it caches that data when accessed, in a mutable member. Now, you could move that object by stealing its cached data, without actually modifying the object. So it would make sense (insofar as it makes sense for the object to be movable at all) for that object to have move constructor and move assignment operator that take const Record&&.

    This is perfectly legal, section 12.8p3 of the C++11 Standard provides that:

    A non-template constructor for class X is a move constructor if its first parameter is of type X&&, const X&&, volatile X&&, or const volatile X&&, and either there are no other parameters or else all other parameters have default arguments.

    and p19:

    A user-declared move assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X&&, const X&&, volatile X&&, or const volatile X&&.

    In this way, you could move a const object, reopen the new copy whatever-you-call-an-instance-created-via-move, thus reusing the buffer space for something else and avoiding a new allocation, while leaving the old object intact and ready to return its content (by hitting the disk or database again) if required.

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

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following code: #include<iostream> #include<vector> using namespace std; class Foo { public: template<
Consider this code: #include <iostream> #include <type_traits> using namespace std; template<typename T_orig> void f(T_orig&
Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
Consider this code: #include <iostream> using namespace std; void Func(int&& i) { ++i; }
Consider the sample code below: #include <iostream> using namespace std; class core { public:
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Consider this example code: #include <iostream> class base { public: base() { std::cout <<
Consider this code : #include <iostream> #include <typeinfo> using namespace std; template<typename T1, typename

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.