diff --git a/08-InitializerList.cpp b/08-InitializerList.cpp index 0157994..e6bea8a 100644 --- a/08-InitializerList.cpp +++ b/08-InitializerList.cpp @@ -20,6 +20,24 @@ void f(T (&c)[N]){ std::cout << std::endl; } +//List Traversal. +void g(std::initializer_list l){ + //Method 1 + for(int i = 0; i < l.size(); ++i) + std::cout << l.begin()[i] << " "; + std::cout << std::endl; + + //Method 2 + for(auto p = l.begin(); p != l.end(); ++p) + std::cout << *p << " "; + std::cout << std::endl; + + //Method 3 + for(auto a: l) + std::cout << a << " "; + std::cout << std::endl; +} + int main(){ C c{1, 2, 3}; std::cout << c.data[0] << std::endl; @@ -37,4 +55,6 @@ int main(){ }; f(a); + + g({1, 2, 3, 4, 5}); } \ No newline at end of file diff --git a/48-RuleOfFive.cpp b/48-RuleOfFive.cpp new file mode 100644 index 0000000..ee0a444 --- /dev/null +++ b/48-RuleOfFive.cpp @@ -0,0 +1,58 @@ +//48-RuleOfFive + +#include + +class A{ +public: + A(); //default constructor. + A(int); //user-defined constructor. + ~A(); //destructor. + A(const A&); //copy constructor. + A(A&&); //move constructor. + A& operator=(const A&); //copy assignment. + A& operator=(A&&); //move assignment. + + int a; +}; + +A::A(): a{0}{ + std::cout << "default constructor." << std::endl; +} + +A::A(int x): a{x}{ + std::cout << "user defined constructor." << std::endl; +} + +A::~A(){ + std::cout << "destructor." << std::endl; +} + +A::A(const A& x): a{x.a}{ + std::cout << "copy constructor." << std::endl; +} + +A::A(A&& x): a{x.a}{ + std::cout << "move constructor." << std::endl; +} + +A& A::operator=(const A& x){ + A t{x.a}; + std::cout << "copy assignment." << std::endl; + return t; +} + +A& A::operator=(A&& x){ + A t{x.a}; + std::cout << "move assignment." << std::endl; + return t; +} + +int main(){ + A a; + A b{1}; + A c{a}; + A d{}; + + a = b; + d = std::move(a); +} \ No newline at end of file diff --git a/49-ExplicitDestructor.cpp b/49-ExplicitDestructor.cpp new file mode 100644 index 0000000..cdafef0 --- /dev/null +++ b/49-ExplicitDestructor.cpp @@ -0,0 +1,32 @@ +//49-ExplicitDestructor + +#include + +class A{ +public: + A(); + //~A(); //implicit destructor. + void destroy(); +private: + ~A(); //explicit destructor. won't be invoked automatically +}; + +A::A(){ + std::cout << "constructor." << std::endl; +} + +A::~A(){ + std::cout << "destructor." << std::endl; +} + +void A::destroy(){ + this->~A(); +} + +int main(){ + //A a; //error. + A* a = new A(); + + //delete a; //error. + a->destroy(); +} \ No newline at end of file diff --git a/MODERNCPP_REMINDER.txt b/MODERNCPP_REMINDER.txt index 2964f40..ff2bf93 100644 --- a/MODERNCPP_REMINDER.txt +++ b/MODERNCPP_REMINDER.txt @@ -100,6 +100,7 @@ * Postcondition: condition that must always be true just after execution of code. * Assertion: condition connected to point in program, that always should evaluate to true at that point in code execution. * PIMPL: pointer to implementation. +* Rule of Five: if class has destructor that performs nontrivial task, such as free-store deallocation or lock release, class is likely to need full complement of functions: user-defined/copy/move constructors, copy/move assignments. [C++ ENTITIES] @@ -379,12 +380,19 @@ * declare constructor that can be called with single argument explicit. * const does not apply (transitively) to objects accessed through pointers or references. * nested class can refer to types and static members of its enclosing class, but has no notion of current object of enclosing class. - * Nifty Counter Idiom: -* Return Value Optimization: * Dynamic Binding During Initialization Idiom: calling virtuals during initialization. * Don’t pass arrays as pointers, pass object representing range. * Prefer struct as parameter type rather than long argument list. * Prefer abstract classes as interfaces to class hierarchies. +* Separate interface of class from its implementation. +* Destructor will be implicitly invoked whenever X goes out of scope or is deleted. +* Destruction can be prevented by declaring destructor =delete or private. +* virtual destructor in base class makes it possible to delete appropriate derived class destructor. +* default constructor disappears when you define constructor requiring arguments. copy constructor does not disappear. +* references and consts must be initialized. + * Nifty Counter Idiom: +* Return Value Optimization: + [ISO CPP GUIDELINESS] diff --git a/README.md b/README.md index 6519aac..27d1f03 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,6 @@ - 44-StaticMember - 45-TemplateSpecialization - 46-SFINAE -- 47-TemplateDerivedClass \ No newline at end of file +- 47-TemplateDerivedClass +- 48-RuleOfFive +- 49-ExplicitDestructor \ No newline at end of file