tailieunhanh - HandBooks Professional Java-C-Scrip-SQL part 51
Tham khảo tài liệu 'handbooks professional java-c-scrip-sql part 51', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | noncopyable Header boost The compiler is often a very good friend of the programmer but not always. One example of its friendliness is the way that it automatically provides copy construction and assignment for our classes should we decide not to do so ourselves. This can lead to some unpleasant surprises if the class isn t meant to be copied or assigned to in the first place. When that s the case we need to tell clients of this class explicitly that copy construction and assignment are prohibited. I m not talking about comments in the code but about denying access to the copy constructor and copy assignment operator. Fortunately the compiler-generated copy constructor and copy assignment operator are not usable when the class has bases or data members that aren t copyable or assignable. boost noncopyable works by prohibiting access to its copy constructor and assignment operator and then being used as a base class. Usage To make use of boost noncopyable have the noncopyable classes derive privately from it. Although public inheritance works too this is a bad practice. Public inheritance says IS-A denoting that the derived class also IS-A base to people reading the class declaration but stating that a class IS-A noncopyable seems a bit far fetched. Include boost utility. hpp when deriving from noncopyable. include boost class please_dont_make_copies boost noncopyable int main please_dont_make_copies d1 please_dont_make_copies d2 d1 please_dont_make_copies d3 d3 d1 The preceding example does not compile. The attempted copy construction of d2 fails because the copy constructor of noncopyable is private. The attempted assignment of dl to d3 fails because the copy assignment operator of noncopyable is private. The compiler should give you something similar to the following output In copy constructor please_dont_make_copies please_dont_make_copies const please_dont_make_copies boost 27 error boost noncopyable .
đang nạp các trang xem trước