Last active
July 29, 2019 05:38
-
-
Save qiaoxu123/8a5641fcd708a46472c423be3fe43c07 to your computer and use it in GitHub Desktop.
`default`函数存在的问题,在于用户想要自定义函数进行类的初始化。此时类的默认构造函数需要显示进行定义。这是用户自定义的无参数的默认构造函数
此时定义的默认构造函数的功能和类模板原始的自定义构造函数的功能是相同的,但是效率变得非常低。在这使用`default`关键字,将用户自定义的无参数
的构造函数进行处理,这样编译器在处理的时候可以极大地提升效率。 --- [参考链接](https://www.ibm.com/developerworks/cn/aix/library/1212_lu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class base { | |
public: | |
base() = default; | |
~base() = default; | |
private: | |
int num; | |
}; | |
int main() { | |
cout << "Hello World" << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class X { | |
public: | |
virtual ~X() = default; | |
private: | |
int x; | |
}; | |
class Y:public X { | |
private: | |
int y; | |
}; | |
int main(){ | |
X* x = new Y; | |
delete(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment