To prevent a class from copy or copy assignment behavior, there are two ways, as I know,
1st , private inherit from a un-copy-able class, ECPP, Item6
2nd , use macro
Example:
// Uncopyable.h
#pragma once
class Uncopyable
{
protected: // allow construction
Uncopyable() {} // and destruction of
~Uncopyable() {} // derived objects...
private:
Uncopyable(const Uncopyable&); // ...but prevent copying
Uncopyable& operator=(const Uncopyable&);
};
#define UN_COPY_ABLE(classname) /
private: /
classname(const classname&); /
classname& operator=(const classname&);
//Item6.cpp
#include "Uncopyable.h"
class HomeForSale : private Uncopyable { // class no longer
// ... // declares copy ctor or
}; // copy assign. operator
class HomeToBuy
{
public:
HomeToBuy(){}; // has to declare ctor
UN_COPY_ABLE(HomeToBuy)
};
template<class T>
void testuncopyable()
{
T t1;
//T t2(t1);
T t2;
//t2 = t1;
}
//
int main(void)
{
testuncopyable<HomeForSale>();
testuncopyable<HomeToBuy>();
return 0;
}