标签:
1. 当声明一个类时,编译器会自动为该类生成默认构造函数,复制构造函数,赋值操作符以及析构函数;
2.自动生成的各个函数和操作符都是public的;
3.当声明一个类不允许复制时,可以将一个类的复制构造函数和赋值操作符声明为private,但是实际中,一般写一个noncopyable类,让不允许使用复制构造函数的类继承于该noncopyable类,boost类就实现了那么一个基类,代码很简单如下:
// Boost noncopyable.hpp header file --------------------------------------// // (C) Copyright Beman Dawes 1999-2003. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // See http://www.boost.org/libs/utility for documentation. #ifndef BOOST_CORE_NONCOPYABLE_HPP #define BOOST_CORE_NONCOPYABLE_HPP #include <boost/config.hpp> namespace boost { // Private copy constructor and copy assignment ensure classes derived from // class noncopyable cannot be copied. // Contributed by Dave Abrahams namespace noncopyable_ // protection from unintended ADL { class noncopyable { protected: #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) && !defined(BOOST_NO_CXX11_NON_PUBLIC_DEFAULTED_FUNCTIONS) BOOST_CONSTEXPR noncopyable() = default; ~noncopyable() = default; #else noncopyable() {} ~noncopyable() {} #endif #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) noncopyable( const noncopyable& ) = delete; noncopyable& operator=( const noncopyable& ) = delete; #else private: // emphasize the following members are private noncopyable( const noncopyable& ); noncopyable& operator=( const noncopyable& ); #endif }; } typedef noncopyable_::noncopyable noncopyable; } // namespace boost #endif // BOOST_CORE_NONCOPYABLE_HPP
模拟:
// // main.cpp // nocopy // // Created by busyfisher on 13-6-17. // Copyright (c) 2013年 busyfisher. All rights reserved. // #include <iostream> #include <string> class nocopyable{ protected: nocopyable(){} ~nocopyable(){} private: nocopyable(const nocopyable&); const nocopyable& operator =(const nocopyable&); }; class person:public nocopyable{ public: person():age(0){} void set_name(const std::string& str){ name = str; } void set_age(const int& _age){ age = _age; } private: int age; std::string name; }; int main(){ person p1; p1.set_age(20); p1.set_name("Liming"); person p2(p1);<span style="font-family: Arial, Helvetica, sans-serif;">//错误,call to implicitly-deleted copy constructor of ‘person‘</span><span style="font-family: Arial, Helvetica, sans-serif;">}</span>
nocopyable.cpp: In copy constructor ‘Person::Person(const Person&)’:
nocopyable.cpp:10:2: error: ‘noncopyable::noncopyable(const noncopyable&)’ is private
noncopyable(const noncopyable&);
上面继承不应该是Public,参考下面的:
一个疑惑:
noncopyable类中成员函数只有声明没有定义,行么?
Effective C++
item 06
“为了拒绝编译器自动提供的功能,将相应的函数声明为 private,而且不要给出实现”
网上有版本
C++箴言:拒绝不想用的编译器产生的函数
http://c.chinaitlab.com/cc/ccjq/200808/761013.html
刚试了一下
声明而不定义适用于private及protected的函数,但是public不行
用的Dev-C++,对它们(//注释行)的调用都是linker error,但是仅编译而不连接都可以通过
看来不管访问权限,声明而不定义对编译阶段没有影响,实际的检测是连接阶段
不知道总结得对不对。
C++箴言:拒绝不想用的编译器产生的函数
HomeForSale h1; HomeForSale h2; HomeForSale h3(h1); // attempt to copy h1 - should // not compile! h1 = h2; // attempt to copy h2 - should // not compile! |
class HomeForSale { public: .. private: ... HomeForSale(const HomeForSale&); // declarations only HomeForSale& operator=(const HomeForSale&); }; |
class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects... private: Uncopyable(const Uncopyable&); // ...but prevent copying Uncopyable& operator=(const Uncopyable&); }; |
class HomeForSale: private Uncopyable { // class no longer ... // declares copy ctor or }; // copy assign. operator |
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; }
标签:
原文地址:http://www.cnblogs.com/youxin/p/4269401.html