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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:56:54+00:00 2026-06-03T20:56:54+00:00

Is there a way to allow a move constructor and disallow copy construction and

  • 0

Is there a way to allow a move constructor and disallow copy construction and assignment. I can think of several classes with file pointers and buffer pointers (resource handles etc) that would benefit from being copy constructed and assigned.

I am using VC2010 & GCC 4.5.2. I know that I would have to declare empty private assignment and copy constructors in the VC2010 class headers and as far as I am aware GCC allows some sort of delete signature after the method to do the same thing.

If anyone has a good example of a skeleton class like this and the advantages I would be very grateful.
Thanks in advance
John

Here is an example of a class for which I would like to allow moves but I would also like to prevent direct asssignment. Is it simily a matter of making the copy constructor and operator=private?

class LoadLumScanner_v8002 : public ILoadLumScanner { 
public:
// default constructor
LoadLumScanner_v8002();

// copy constructor
LoadLumScanner_v8002(const LoadLumScanner_v8002& rhs);

// move constructor
LoadLumScanner_v8002(LoadLumScanner_v8002&& rhs);

// non-throwing copy-and-swap idiom unified assignment
inline LoadLumScanner_v8002& operator=(LoadLumScanner_v8002 rhs) {
    rhs.swap(*this);
    return *this;
}

// non-throwing-swap idiom
inline void swap(LoadLumScanner_v8002& rhs) throw() {
    // enable ADL (not necessary in our case, but good practice)
    using std::swap;
    // swap base members
    // ... 
    // swap members
    swap(mValidatedOk, rhs.mValidatedOk);
    swap(mFile, rhs.mFile);
    swap(mPartNo, rhs.mPartNo);
    swap(mMediaSequenceNo, rhs.mMediaSequenceNo);
    swap(mMaxMediaSequenceNo, rhs.mMaxMediaSequenceNo);
    swap(mLoadListOffset, rhs.mLoadListOffset);
    swap(mFirstLoadOffset, rhs.mFirstLoadOffset);
    swap(mLoadCount, rhs.mLoadCount);
    swap(mLoadIndex, rhs.mLoadIndex);
    swap(mLoadMediaSequenceNo, rhs.mLoadMediaSequenceNo);
    swap(mLoadPartNo, rhs.mLoadPartNo);
    swap(mLoadFilePath, rhs.mLoadFilePath);
}

// destructor
virtual ~LoadLumScanner_v8002();
}
  • 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-03T20:56:55+00:00Added an answer on June 3, 2026 at 8:56 pm

    Both of the two solutions you mention work fine.

    1.

    class MoveOnly
    {
       MoveOnly(const MoveOnly&);
       MoveOnly& operator=(const MoveOnly&);
    public:
       MoveOnly(MoveOnly&&);
       MoveOnly& operator=(MoveOnly&&);
    };
    
    class MoveOnly
    {
    public:
       MoveOnly(const MoveOnly&) = delete;
       MoveOnly& operator=(const MoveOnly&) = delete;
       MoveOnly(MoveOnly&&) = default;
       MoveOnly& operator=(MoveOnly&&) = default;
    };
    

    The "= delete" signature is new with C++11 (as is the rvalue reference), and means essentially the same thing as the C++03 technique (declare private and don’t define). The advantage of the C++11 solution is that it will for sure catch mistakes at compile time, not delay until link time.

    Your compiler may not yet support "= delete" and in that case, you’ll have to fall back on the first solution.

    A third solution is to default the copy members:

    class MoveOnly
    {
    public:
       MoveOnly(MoveOnly&&) = default;
       MoveOnly& operator=(MoveOnly&&) = default;
    };
    

    When a move special member is declared, whether defaulted or not, then the compiler will implicitly add deleted copy members if you do not declare them otherwise. Your compiler may or may not implement this feature yet.

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

Sidebar

Related Questions

Is there a way to allow an HTML file to open an application on
Using htmlentities() is there a way I can set to allow only <b> and
I'm hoping there's a way in a config file or something that will allow
Is there a way to allow a user, after he has created a vector
Is there a way to allow the user to add additional items to one
Using SQL Server 2008, is there a way to allow inserts to a table
Is there any way to create a control that will allow its children controls
Is there a way in objective-c/Cocoa to alloc an object when the class name
Windows allows configuration of Measurement system to Metric or U.S. Is there a way
is there way how to get name ov event from Lambda expression like with

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.