简体   繁体   English

=删除用户定义的成员函数,除了构造函数,赋值运算符c ++ 11

[英]=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
}
  • Provide stricter input arguments checks: 提供更严格的输入参数检查:

 void foo(void *){}

 void foo(int) = delete;

 foo(0); // error
  • Mark methods as "explicitly not implemented", this may be useful when a family of classes has a certain function but this particular class does not provide it for performance or other reasons: 将方法标记为“未明确实现”,当一类类具有某些功能但由于性能或其他原因而无法提供该功能时,这可能会很有用:

class my_list
{
    // you should use other container if you need constant time size
    public: size_t size(void) = delete;     
};
  • As a final step of function deprecation. 作为功​​能弃用的最后一步。 Some even use a dedicated macro. 有些甚至使用专用宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++ 11移动构造函数和赋值运算符 - C++11 move constructor and assignment operator 为什么在 C++11 或 C++14 中,当我声明移动赋值运算符时,编译器会隐式删除复制构造函数? - Why in C++11 or C++14 does the compiler implicitly delete the copy constructor when I declare a move assignment operator? 用户定义 C++11 枚举 class 默认构造函数 - User Defined C++11 enum class Default Constructor C ++ 11字符串赋值运算符 - C++11 string assignment operator 来自 initializer_list 的三元运算符 + C++11 构造函数 - Ternary operator + C++11 constructor from initializer_list C ++ 11构造函数和运算符的统一初始化= - C++11 Uniform initialization for constructor and operator= 为什么C ++ 11的move构造函数/赋值运算符不按预期运行 - Why do not C++11's move constructor/assignment operator act as expected 从c ++ 11中的用户定义的文字返回一个std :: array - returning a std::array from a user defined literal in c++11 C ++ 11中用户定义的属性? - User-defined attributes in C++11? 将赋值运算符从基类引入派生对象(C++11 之前) - Bring to derived object the assignment operator from base (prior to C++11)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM