C++"View() = default"是什么?

What is "View() = default" in C++?

本文关键字:default 是什么 View C++      更新时间:2023-10-16

这是我在C++中从未见过的语法。

请参阅以下内容:

class View
{
    private:
    int screenSize;
    int screenScale; //"the ZOOM"
    Point origin;
public:
    const int minScreenSize = 6;
    const int maxScreenSize = 30;
    View():screenSize(25),screenScale(2),origin(-10,-10){}
    ~View() = default;
    View(const View&) = default;
    View(View&&) = default;
    View& operator=(const View&) = default;
    View& operator=(View&&) = default;
    View& myAdd() = delete;
}

是什么意思:

View() = defaultView() = delete

提前谢谢。

它告诉编译器为您的类生成默认的"默认构造函数"。

这是在 C++11 中引入的。

对应的是= delete;,它将指示编译器生成函数。

= default;比空的用户定义的 consteuctor {}更可取,因为根据定义,使用的已定义析构函数永远不会trivial(即使它们是空的),但编译器生成的函数是。

更多细节在这里:

http://en.cppreference.com/w/cpp/language/member_functions#Special_member_functions

http://en.cppreference.com/w/cpp/language/default_constructor