Created
January 27, 2016 12:56
-
-
Save benjaminparnell/6d13954625d95934554b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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