From fccd0111a386a4a17aaa2191c53343313f0ee829 Mon Sep 17 00:00:00 2001 From: lion187 Date: Wed, 19 Jun 2019 16:27:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6=20Cpp=5F?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=9E=84=E9=80=A0=E6=96=B9=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Program/Language/Cpp/Cpp_默认构造方法.md | 99 ++++++++++++++++++- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/Software/Program/Language/Cpp/Cpp_默认构造方法.md b/Software/Program/Language/Cpp/Cpp_默认构造方法.md index 7ee8701..497ca87 100644 --- a/Software/Program/Language/Cpp/Cpp_默认构造方法.md +++ b/Software/Program/Language/Cpp/Cpp_默认构造方法.md @@ -68,11 +68,104 @@ int main() } ``` -C++11中,当类中含有不能默认初始化的成员变量时,可以禁止默认构造函数的生成: +为了避免手动编写空默认构造函数,C++11引入了显示默认构造函数的概念,从而只需在类的定义中编写空默认构造函数而不需要在实现文件中提供其实现: ```cpp -myClass()=delete; //表示删除默认构造函数 -myClass()=default; //表示默认存在构造函数 +// A.h +#ifndef A_H +#define A_H + +class A{ +public: + A()=default; //default + A(int ii); + void show()const; + +private: + int i; +}; + +#endif // A_H +``` + +```cpp +// A.cpp +#include +#include "tc.h" + +using namespace std; + +/* +A::A() +{ + // 不必给出其实现 +}; +*/ + +A::A(int ii) +{ + i=ii; +} + +void A::show()const +{ + std::cout<<"this is A object!"< +#include "tc.h" + +using namespace std; + +/* +A::A() +{ + // 不必给出其实现 +} + +A::A(int ii) +{ + i=ii; +}*/ + +void A::show()const +{ + std::cout<<"this is A object!"<