码迷,mamicode.com
首页 > 其他好文 > 详细

(五)noncopyable禁止拷贝类

时间:2018-06-11 00:28:48      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:cpp   AC   follow   auth   The   ace   编译   const   ons   

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;
}

(五)noncopyable禁止拷贝类

标签:cpp   AC   follow   auth   The   ace   编译   const   ons   

原文地址:https://www.cnblogs.com/walkinginthesun/p/9164999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!