编程参考 - C++的default使用

在 C++ 中,= default 用于明确声明特殊成员函数应使用编译器提供的默认实现。它可用于构造函数、析构函数、复制构造函数、复制赋值运算符、移动构造函数和移动赋值运算符。

In C++, = default is used to explicitly declare that a special member function should use the default implementation provided by the compiler. This can be applied to constructors, destructors, copy constructors, copy assignment operators, move constructors, and move assignment operators.

以下是一些使用 = default 的常见情况:

Here are some common scenarios where = default is used:

1. 构造函数:

1. Constructor:

class MyClass {

public:

    MyClass() = default;

};

声明应使用默认构造函数。

This declares that the default constructor should be used.

2. 析构函数:

2. Destructor:

class MyClass {

public:

    ~MyClass() = default;

};

声明应使用默认的析构函数。

This declares that the default destructor should be used.

3. 拷贝构造函数:

3. Copy Constructor:

class MyClass {

public:

    MyClass(const MyClass&) = default;

};

声明使用默认的拷贝构造函数。

This declares that the default copy constructor should be used.

4. 拷贝赋值操作符:

4. Copy Assignment Operator:

class MyClass {

public:

    MyClass& operator=(const MyClass&) = default;

};

声明使用默认的拷贝赋值操作符。

This declares that the default copy assignment operator should be used.

5. 移动构造函数:

5. Move Constructor:

class MyClass {

public:

    MyClass(MyClass&&) = default;

};

此处声明应使用默认的移动构造函数。

This declares that the default move constructor should be used.

6. 移动赋值操作符:

6. Move Assignment Operator:

class MyClass {

public:

    MyClass& operator=(MyClass&&) = default;

};

This declares that the default move assignment operator should be used.

此处声明使用默认的移动赋值运算符。

Why Use = default?

* 明确性:明确表示希望使用默认行为。

* 效率:在某些情况下,编译器生成的函数可能比手动编写的函数更有效率。

* 隐式声明:  显式使用 = default 时,编译器不会隐式声明特殊成员函数。当你想控制哪些特殊成员函数可用以及它们的行为方式时,这一点尤其有用。

* 强制指定:如果没有声明这些函数,编译器就不会生成它们。如果要确保编译器不会隐式生成这些函数,可以将它们声明为delete。

* Clarity: It makes the intention explicit that the default behavior is desired.

* Efficiency: In some cases, the compiler-generated function can be more efficient than a manually written one.

* Implicit Declarations:  When you explicitly use = default, it prevents the compiler from making implicit declarations of special member functions. This is particularly useful when you want to control which special member functions are available and how they behave.

* Enforcement: It prevents the compiler from generating these functions if they are not declared. If you want to ensure that the compiler does not generate these functions implicitly, you can declare them as deleted.

Example

下面是一个完整的示例,演示了 = default的使用:

Here’s a full example demonstrating the use of = default:

#include <iostream>

class MyClass {

public:

    MyClass() = default;

    ~MyClass() = default;

    MyClass(const MyClass&) = default;

    MyClass& operator=(const MyClass&) = default;

    MyClass(MyClass&&) = default;

    MyClass& operator=(MyClass&&) = default;

    void display() {

        std::cout << "MyClass instance" << std::endl;

    }

};

int main() {

    MyClass obj1;

    MyClass obj2 = obj1;  // Copy constructor

    MyClass obj2(obj1);  // Copy constructor

    MyClass obj3 = std::move(obj1);  // Move constructor

    MyClass obj3(std::move(obj1));  // Move constructor

    obj2 = obj3;  // Copy assignment

    obj3 = std::move(obj2);  // Move assignment

    obj3.display();

    return 0;

}

在本例中,类 MyClass 使用 = default 来表示应使用特殊成员函数的默认实现。这样既保持了代码的简洁,又明确了程序员的意图。

In this example, the class MyClass uses = default to indicate that the default implementations of the special member functions should be used. This keeps the code concise and makes the programmer's intentions clear.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值