比如在自定义类中 为了阻止其他程序员使用 拷贝构造,等函数。我们常用的用法:
将该函数定义为private 不去定义只是声明, 或者delete.
区别一:
delete 可以修饰任何函数
private 只能修饰类(类对象)的成员函数。
区别二:
删除函数无法通过任何方法使用, 即使是类的成员函数 和 友元 也不行。
private修饰的是可以 通过类的成员 以及友元 去调用, 但调用的时候会出现该函数未定义。
区别三:
在模板特化的情况下
class TestDelete{
public:
template<class T>
void processPointer(T* ptr){
//...
}
private:
template<class T>
void processPointer<void>(void*);//error: function template partial specialization is not allowed
};
class TestDelete{
public:
template<class T>
void processPointer(T* ptr){
std::cout<<"processPoniter";
}
};
template<>
void TestDelete::processPointer<void>(void*) = delete;
int main(){
TestDelete t;
t.processPointer<void>(nullptr);//error: call to deleted member function 'processPointer'
}