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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:04:19+00:00 2026-06-03T02:04:19+00:00

What would be the correct way to implement a move constructor considering the following

  • 0

What would be the correct way to implement a move constructor considering the following class:

class C {
public:
    C();
    C(C&& c);
private:
    std::string string;
}

Of course, the idea is to avoid copying string or deallocating it twice.
Lets assume the basic example is just for clarity and I do need a move constructor.


I tried:

C::C(C&& c) {
    //move ctor
    string = std::move(c.string);
}

And

C::C(C&& c) : string(std::move(c.string)) {
    //move ctor
}

Both compile fine on gcc 4.8 and run fine. It seems option A is the correct behaviour, string gets copied instead of moved with option B.
Is this the correct implementation of a move constructor?

  • 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-03T02:04:20+00:00Added an answer on June 3, 2026 at 2:04 am

    Since std::string itself has a move-ctor, the implicitly defined move-ctor for C will take care of the proper move operation. You may not define it yourself. However, if you have any other data member and specifically:

    12.8 Copying and moving class objects

    12 An implicitly-declared copy/move constructor is an inline public
    member of its class. A defaulted copy- /move constructor for a class X
    is defined as deleted (8.4.3) if X has:

    — a variant member with a
    non-trivial corresponding constructor and X is a union-like class,

    — a
    non-static data member of class type M (or array thereof) that cannot
    be copied/moved because overload resolution (13.3), as applied to M’s
    corresponding constructor, results in an ambiguity or a function that
    is deleted or inaccessible from the defaulted constructor, or

    — a
    direct or virtual base class B that cannot be copied/moved because
    overload resolution (13.3), as applied to B’s corresponding
    constructor, results in an ambiguity or a function that is deleted or
    inaccessible from the defaulted constructor, or

    — for the move
    constructor, a non-static data member or direct or virtual base class
    with a type that does not have a move constructor and is not trivially
    copyable.

    13 A copy/move constructor for class X is trivial if it is
    neither user-provided nor deleted and if

    — class X has no virtual functions (10.3) and no virtual base classes (10.1), and
    functions (10.3) and no virtual base classes (10.1), and

    — the
    constructor selected to copy/move each direct base class subobject is
    trivial, and

    — for each non-static data member of X that is of class
    type (or array thereof), the constructor selected to copy/move that
    member is trivial; otherwise the copy/move constructor is non-trivial.

    you may want to implement your own move-ctor.

    In case you need the move-ctor, prefer the initializer list syntax. Always! Otherwise, you may end up with a default construction per object not mentioned in the initializer list (which is what you’re forced for member objects with non-default ctors only).

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

Sidebar

Related Questions

Would the following be the correct way to implement a fairly straightforward thread-safe logging
What would be correct way to create new instance of the class from class
Would be correct the next way to implement the methods attached to an interface?
What is the correct way to implement a constructor in android? It seems that
What would be the correct way to create a fully URL-encoded file:// URI from
What would be the correct way to add DISTINCT and/or GROUPBY to ContentResolver -based
suppose i have a sequence, called TEST_SEQ what would be the correct way of
Would this be the correct way of declaring that an XML element Cluster contains
Would it be possible to implement a custom Thread class in Java (using JNI)
I was wondering what is the correct way to implement multiple choices in django-models.

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.