c++ - =删除用户定义的成员函数,除了构造函数,赋值运算符c ++ 11
问题描述
在 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 {};
};
在上面的示例中,我在用户定义的成员函数上使用了 delete。它说我们可以定义paint() 函数,因此没有其他函数可以调用它。
想知道是否存在这种类型的函数声明(绘制)有用/推荐的场景。
解决方案
因此,这种过载没有任何好处。
#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
}
推荐阅读
- javascript - 纱线安装命令时出现 ESOCKETTIMEDOUT 错误
- java - 如何管理对象数组?
- javascript - 你如何通过 props 传递一个字符串,然后在 React Native 的组件标签中使用它作为一个值?
- c++ - stl:priority_queue 未按 FIFO 顺序对相同优先级元素进行排序
- python - 如何在熊猫框架中将特定月份值与日期相加?
- python - 在时钟秒值更改的精确时刻调用 Python 子例程?
- python - 使用 Scrapy 和 Python 进行分页抓取
- javascript - 我怎样才能使我的导航内容居中?
- azure - 无法使用 Azure 容器实例的 dns 解析 IP
- java - Hibernate Search - 通配符和空格