Skip to content

Instantly share code, notes, and snippets.

@benjaminparnell
Created January 27, 2016 12:56
Show Gist options
  • Save benjaminparnell/6d13954625d95934554b to your computer and use it in GitHub Desktop.
Save benjaminparnell/6d13954625d95934554b to your computer and use it in GitHub Desktop.
struct NotCopyable {
// defaulted and deleted funtions are only available in C++11
NotCopyable(const NotCopyable&) = delete;
// Overriding the = operator so you can't copy one NotCopyable to another
// one
NotCopyable& operator=(const NotCopyable&) = delete;
// You would need the following line to be able to construct an instance of this
// struct at all
/* NotCopyable() = default; */
};
int main() {
// Not only can you not copy this, you can't make an instance of it
// with new either
/* NotCopyable nc1 = new NotCopyable(); */
// Which means you can't do this
NotCopyable nc2;
// Or this
struct NotCopyable nc3;
// This code won't actually compile at all (on GCC at least)
// If you include the constructor line at line 11 in this file, this code will
// compile until it hits this line. We have deleted the equals operator, so
// it still would not compile
/* nc2 = nc3; */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment