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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:46:07+00:00 2026-05-21T09:46:07+00:00

Is this class design the standard C++0x way to prevent copy and assign ,

  • 0

Is this class design the standard C++0x way to prevent copy and assign, to protect client code against accidental double-deletion of data?

struct DataHolder {
  int *data;   // dangerous resource
  DataHolder(const char* fn); // load from file or so
  DataHolder(const char* fn, size_t len); // *from answers: added*
  ~DataHolder() { delete[] data; }

  // prevent copy, to prevent double-deletion
  DataHolder(const DataHolder&) = delete;
  DataHolder& operator=(const DataHolder&) = delete;

  // enable stealing
  DataHolder(DataHolder &&other) {
    data=other.data; other.data=nullptr;
  }
  DataHolder& operator=(DataHolder &&other) {
    if(&other!=this) { data = other.data; other.data=nullptr};
    return *this;
  }
};

You notice, that I defined the new move and move-assign methods here. Did I implement them correctly?

Is there any way I can — with the move and move-assign definitions — to put DataHolder in a standard container? like a vector? How would do I do that?

I wonder, some options come into mind:

// init-list. do they copy? or do they move?
// *from answers: compile-error, init-list is const, can nor move from there*
vector<DataHolder> abc { DataHolder("a"), DataHolder("b"), DataHolder("c") };

// pushing temp-objects.
vector<DataHolder> xyz;
xyz.push_back( DataHolder("x") );
// *from answers: emplace uses perfect argument forwarding*
xyz.emplace_back( "z", 1 );

// pushing a regular object, probably copies, right?
DataHolder y("y");
xyz.push_back( y ); // *from anwers: this copies, thus compile error.*

// pushing a regular object, explicit stealing?
xyz.push_back( move(y) );

// or is this what emplace is for?
xyz.emplace_back( y ); // *from answers: works, but nonsense here*

The emplace_back idea is just a guess, here.

Edit: I worked the answers into the example code, for readers convenience.

  • 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-21T09:46:08+00:00Added an answer on May 21, 2026 at 9:46 am

    Your example code looks mostly correct.

    1. const DataHolder &&other (in two places).

    2. if(&other!=this) in your move assignment operator looks unnecessary but harmless.

    3. The initializer list vector constructor won’t work. This will try to copy your DataHolder and you should get a compile time error.

    4. The push_back and emplace_back calls with rvalue arguments will work. Those with lvalue arguments (using y) will give you compile time errors.

    There really is no difference between push_back and emplace_back in the way that you have used them. emplace_back is for when you don’t want to construct a DataHolder outside of the vector, but instead pass the arguments to construct one only inside the vector. E.g.:

    // Imagine this new constructor:
    DataHolder(const char* fn, size_t len);
    
    xyz.emplace_back( "data", 4 );  // ok
    xyz.push_back("data", 4 );  // compile time error
    

    Update:

    I just noticed your move assignment operator has a memory leak in it.

    DataHolder& operator=(DataHolder &&other)
    {
       if(&other!=this)
       {
          delete[] data;  // insert this
          data = other.data;
          other.data=nullptr;
       }
       return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen this question: Class design with vector as a private/public member? , but
I was reading up on singleton class design in C# on this great resource
I'm using this php class ( http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php ) for creating the different sized images
Is this good OO Design assuming you want every inheriting class to be a
Given this class: class Foo { readonly ILog log; public Foo(ILog log) { this.log
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
Consider this class hierarchy: Book extends Goods Book implements Taxable As we know, there
Consider this: class User < ActiveRecord::Base # name, email, password end class Article <
Given this class public partial class Default : Page { private IRepository repo; ...
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement

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.