Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoklavat committed Jan 29, 2021
1 parent 2f5a117 commit 261fe8e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
20 changes: 20 additions & 0 deletions 08-InitializerList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ void f(T (&c)[N]){
std::cout << std::endl;
}

//List Traversal.
void g(std::initializer_list<int> 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;
Expand All @@ -37,4 +55,6 @@ int main(){
};

f(a);

g({1, 2, 3, 4, 5});
}
58 changes: 58 additions & 0 deletions 48-RuleOfFive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//48-RuleOfFive

#include <iostream>

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);
}
32 changes: 32 additions & 0 deletions 49-ExplicitDestructor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//49-ExplicitDestructor

#include <iostream>

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();
}
12 changes: 10 additions & 2 deletions MODERNCPP_REMINDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@
- 44-StaticMember
- 45-TemplateSpecialization
- 46-SFINAE
- 47-TemplateDerivedClass
- 47-TemplateDerivedClass
- 48-RuleOfFive
- 49-ExplicitDestructor

0 comments on commit 261fe8e

Please sign in to comment.