You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++11中=delete修饰构造、赋值运算符及普通成员函数的作用解析

Using = delete on Custom Member Functions in C++11 (Beyond Constructors/Assignment)

Great question! The = delete specifier isn't just limited to constructors and assignment operators—it's a powerful tool for enforcing intentional behavior in your code when applied to custom member functions. Let's break down its key use cases with examples, including your paint() function scenario:

1. Explicitly Prohibit All Calls to a Function

When you mark a member function as = delete, you're telling the compiler to reject any attempt to call that function—whether it's an explicit call from your code or an implicit call generated by the compiler.

Take your example:

class color{
public:
    virtual void paint() = delete;
};

Any attempt to invoke paint() will throw a compile error:

color c;
c.paint(); // Compile error: function is deleted
color* ptr = &c;
ptr->paint(); // Also a compile error

This is perfect for blocking usage of functions that are obsolete, placeholders, or never meant to be executed in the first place.

2. Block Derived Classes from Overriding Virtual Functions

If you delete a virtual member function in a base class, derived classes cannot override it. The compiler will flag an error if any derived class tries to define a function with the same signature, since the base class has explicitly invalidated that function slot.

For example:

class derived_color : public color {
public:
    void paint() override; // Compile error: cannot override a deleted virtual function
};

This enforces strict design constraints—say you want to ensure certain functionality is never implemented by derived classes, this locks that in at compile time instead of relying on runtime checks or comments.

3. Prevent Unwanted Overload Selection

If you have overloaded member functions, deleting one overload prevents the compiler from selecting it—even if implicit type conversion would normally allow it. This eliminates subtle bugs caused by unintended type conversions.

Example:

class calculator {
public:
    int add(int a, int b) { return a + b; }
    float add(float a, float b) = delete; // Block float version
};

calculator calc;
calc.add(1, 2); // OK
calc.add(1.5f, 2.5f); // Compile error: deleted function
calc.add(1, 2.5f); // Also error: would implicitly convert to float, which is deleted

This ensures only your intended overload is used, avoiding ambiguity or unexpected behavior.

4. Enforce Class Usage Constraints

Deleting non-virtual member functions can restrict how your class is used. For example, you might disable certain methods in a utility class that don't make sense for your use case:

class utils {
public:
    static void log_info(const std::string& msg);
    static void log_debug(const std::string& msg) = delete; // Disable debug logs in release builds
};

utils::log_debug("test"); // Compile error

This is a clean, compile-time way to enforce design decisions without relying on runtime checks.

Bonus: The virtual Difference in Your paint() Example

You noted the virtual keyword isn't required—here's how it changes behavior:

  • Non-virtual + deleted: Derived classes can still define their own paint() method (since it's not an override), but base class instances can't call paint().
  • Virtual + deleted: Derived classes cannot override the function, and no instance (base or derived, via base pointer/reference) can call paint().

内容的提问来源于stack exchange,提问作者Santosh Sahu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:27:15