Boost source learning three [utilities] (1)

Source: Internet
Author: User

This section is some of the more practical gadgets, simple function, code is also easy.

The first one to learn first is noncopyable:

#ifndef boost_noncopyable_hpp_included
#define Boost_noncopyable_hpp_included

namespace BOOST {

//  Private copy constructor and copy assignment ensure classes derived to
//  class noncopyable cannot be Copie D.  contributed by Dave Abrahams

namespace Noncopyable_  //protection from unintended ADL
{
  class Noncopyable
  {
   protected:
      noncopyable () {}
      ~noncopyable () {}
   Private:  //emphasize The following members are private
      noncopyable (const noncopyable&);
      Const noncopyable& operator= (const noncopyable&);}

typedef noncopyable_::noncopyable noncopyable;

} Namespace boost

#endif  //boost_noncopyable_hpp_included

First example:

  Class noncopyable
  {
   protected:
      noncopyable () =default;//default constructors and destructors are protected
      ~noncopyable () =default ;//Use default implementation
   private:  //Emphasize the following members are private; Use the Delete keyword to prohibit copy construction and copy assignment
      noncopyable ( Const noncopyable&) =delete;
      Const noncopyable& operator= (const noncopyable&) =delete;
  


A second example:

Boost::noncopyable is simpler and is mainly used in single cases.
In general, to write a single instance class, you need to have their constructors , assignment functions , destructors , and copy constructors hidden in the class declaration to private or protected . , each class is doing this trouble.
There are noncopyable classes, so long as the single instance class inherits noncopyable directly.
The basic idea of class noncopyable is to set protected permissions on constructors and destructors, such that classes can be invoked, but outside classes cannot be invoked, so it is not impossible to compile when subclasses need to define constructors. But the key is that noncopyable makes the copy constructor and the copy assignment function private, which means that unless the subclass defines its own copy construct and assignment function, if the subclass is undefined, The caller outside is not able to generate a new subclass object by means of assignment and copy construction.

#include "Tfun.h"

class Myclass:public boost::noncopyable
{public
:
	MyClass () {};
	MyClass (int i) {};
};

int main ()
{
	MyClass CL1 ();
	MyClass Cl2 (1);

	MyClass Cl3 (CL1);	Error
	//MyClass Cl4 (Cl2);	Error return

	0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.