C++11中default的使用
在C+11中,對于defaulted函數,編譯器會為其自動生成默認的函數定義體,從而獲得更高的代碼執行效率,也可免除程序員手動定義該函數的工作量。
C++的類有四類特殊成員函數,它們分別是:默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、銷毀,或者拷貝類的對象。如果程序員沒有顯式地為一個類定義某個特殊成員函數,而又需要用到該特殊成員函數時,則編譯器會隱式的為這個類生成一個默認的特殊成員函數。當存在用戶自定義的特殊成員函數時,編譯器將不會隱式的自動生成默認特殊成員函數,而需要程序員手動編寫,加大了程序員的工作量。并且手動編寫的特殊成員函數的代碼執行效率比編譯器自動生成的特殊成員函數低。
C++11標準引入了一個新特性:defaulted函數。程序員只需在函數聲明后加上”=default;”,就可將該函數聲明為defaulted函數,編譯器將為顯式聲明的defaulted函數自動生成函數體。
defaulted函數特性僅適用于類的特殊成員函數,且該特殊成員函數沒有默認參數。
defaulted函數既可以在類體里(inline)定義,也可以在類體外(out-of-line)定義。
In C++11, defaulted and deleted functions give you explicit control over whether the special member functions are automatically generated.
“=default” instructs the compiler to generate the default implementation for the function. Defaulted functions have two advantages: They are more efficient than manual implementations, and they rid the programmer from the chore of defining those functions manually.
By default, C++ will provide a default constructor, copy constructor, copy assignment operator (operator=) and a destructor. If you provide alternate versions of any of these functions for your class, C++ will not provide a default version. However, in C++11, you can now specify that you would like the compiler to provide a default one anyway. This is done by prototyping the function and using the default specifier.
The default specifier can only be used with functions that have a default.
=default: it means that you want to use the compiler-generated version of that function, so you don't need to specify a body.
=delete: it means that you don't want the compiler to generate that function automatically.
下面是從其他文章中copy的測試代碼,詳細內容介紹可以參考對應的reference:
#include "default.hpp"
#include <iostream>// reference: http://www.learncpp.com/cpp-tutorial/b-6-new-virtual-function-controls-override-final-default-and-delete/
class Foo
{Foo(int x); // Custom constructorFoo() = default; // The compiler will now provide a default constructor for class Foo as well
};/
// reference: http://en.cppreference.com/w/cpp/language/default_constructor
struct A
{int x;A(int x = 1) : x(x) {} // user-defined default constructor
};struct B : A
{// B::B() is implicitly-defined, calls A::A()
};struct C
{A a;// C::C() is implicitly-defined, calls A::A()
};struct D : A
{D(int y) : A(y) {}// D::D() is not declared because another constructor exists
};struct E : A
{E(int y) : A(y) {}E() = default; // explicitly defaulted, calls A::A()
};struct F
{int& ref; // reference memberconst int c; // const member// F::F() is implicitly defined as deleted
};int test_default1()
{A a;B b;C c;// D d; // compile errorE e;// F f; // compile errorreturn 0;
}///
// reference: https://msdn.microsoft.com/zh-cn/library/dn457344.aspx
struct widget
{widget() = default;inline widget& operator=(const widget&);
};// Notice that you can default a special member function outside the body of a class as long as it’s inlinable.
inline widget& widget::operator=(const widget&) = default;
GitHub:https://github.com/fengbingchun/Messy_Test
總結
以上是生活随笔為你收集整理的C++11中default的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11中= delete;的使用
- 下一篇: OpenCV代码提取:transpose