Created
August 24, 2014 10:09
-
-
Save tetsuok/c11a04aeb19f8c3fb304 to your computer and use it in GitHub Desktop.
disable copy and assign in C++98 and C++11
This file contains hidden or 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
| // g++ -std=c++11 | |
| class NonCopyable { | |
| public: | |
| NonCopyable() = default; | |
| NonCopyable(const NonCopyable&) = delete; | |
| void operator=(const NonCopyable&) = delete; | |
| private: | |
| friend class A; | |
| }; | |
| class A { | |
| public: | |
| void f() { | |
| NonCopyable a; | |
| NonCopyable b(a); // error | |
| } | |
| void g() { | |
| NonCopyable a; | |
| NonCopyable b = a; // error | |
| } | |
| }; | |
| int main() { | |
| { | |
| NonCopyable a; | |
| // NonCopyable b(a); // error | |
| } | |
| { | |
| NonCopyable a; | |
| // NonCopyable b = a; // error | |
| } | |
| // { | |
| // A a; | |
| // a.f(); | |
| // a.g(); | |
| // } | |
| return 0; | |
| } |
This file contains hidden or 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
| class NonCopyable { | |
| public: | |
| NonCopyable() {} | |
| private: | |
| friend class A; | |
| NonCopyable(const NonCopyable&); | |
| void operator=(const NonCopyable&); | |
| }; | |
| class A { | |
| public: | |
| void f() { | |
| NonCopyable a; | |
| NonCopyable b(a); | |
| } | |
| void g() { | |
| NonCopyable a; | |
| NonCopyable b = a; | |
| } | |
| }; | |
| int main() { | |
| { | |
| NonCopyable a; | |
| // NonCopyable b(a); // error | |
| } | |
| { | |
| NonCopyable a; | |
| // NonCopyable b = a; // error | |
| } | |
| { | |
| A a; | |
| a.f(); | |
| a.g(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment