zoukankan      html  css  js  c++  java
  • (五)noncopyable禁止拷贝类

    1. noncopyable类和copyable类

    /*
     * noncopyable.h
     *
     *  Created on: 2018-6-10
     *      Author: 
     */
    
    #ifndef NONCOPYABLE_H_
    #define NONCOPYABLE_H_
    
    namespace ld{
    
    class noncopyable
    {
    public:
    	noncopyable() {}
    	~noncopyable() {}
    
    private:  // emphasize the following members are private
    	noncopyable( const noncopyable& );
    	noncopyable& operator=( const noncopyable& );
    };
    
    
    class copyable  // emphasize the class is copyalbe
    {
    
    };
    
    } // end of namespace ld
    
    #endif /* NONCOPYABLE_H_ */
    
    

    2. test

    #include <iostream>
    #include "noncopyable.h"
    
    class A : public ld::noncopyable
    {
    public:
    	int a=1;
    };
    
    class B : public ld::copyable
    {
    public:
    	int b=3;
    };
    
    int main()
    {
    	std::cout<<"test noncopyable"<<std::endl;
    	A a;
    	// A a_copy = a;  // 编译失败
    	B b;
    	B b_copy = b;
    	std::cout<<a.a/*<<a_copy.a*/<<b.b<<b_copy.b<<std::endl;
    }
    
  • 相关阅读:
    Vue(知识讲解)
    爬虫框架:scrapy
    爬虫性能相关
    MongoDB
    Beautifulsoup模块
    selenium模块
    requests模块
    爬虫(集锦)
    爬虫目录
    Flask目录
  • 原文地址:https://www.cnblogs.com/walkinginthesun/p/9164999.html
Copyright © 2011-2022 走看看