[英]=delete for user defined member functions apart from constructor, assignment operator c++11
In C++11, we use "= delete" as not to allow the constructors and operator overloaded member functions to be invoked implicitly while doing some operations(change of dataype/assignment of objects). 在C ++ 11中,我们使用“ = delete”是为了避免在执行某些操作(更改数据类型/对象分配)时隐式调用构造函数和运算符重载成员函数。
class color{
public:
color(){cout<<"color constructed called"<<endl;}
color(int a){};
color(float)=delete;
color& operator = (color &a) = delete;
virtual void paint() = delete; //What is the use of delete in this function
//void paint() = delete; The above virtual is not mandatory, just a generic scenario.
virtual void paints () final {};
};
I have used delete on user defined member function in the above example. 在上面的示例中,我已经在用户定义的成员函数上使用了delete。 It says that we can define the paint() function, hence no other function can call it.
它说我们可以定义paint()函数,因此没有其他函数可以调用它。
Want to know if there is there is any scenarios in which this type of function declaration(paint) would be useful/recommended. 想知道是否存在使用/推荐这种类型的函数声明(绘画)的场景。
So that nothing benefits from this overload. 因此,没有任何东西可以从过载中受益。
#include <iostream>
struct Nyan {
int omg(int x) { return x + 2; }
};
struct Meow {
int omg(int x) { return x + 2; }
int omg(double) = delete;
};
int main() {
Nyan n;
Meow m;
std::cout << n.omg(40) << std::endl;
std::cout << m.omg(40) << std::endl;
std::cout << n.omg(40.5) << std::endl;
// std::cout << m.omg(40.5) << std::endl; // commented out for a reason
}
void foo(void *){}
void foo(int) = delete;
foo(0); // error
class my_list
{
// you should use other container if you need constant time size
public: size_t size(void) = delete;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.