Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoklavat committed Feb 1, 2021
1 parent 0452baa commit 83619ca
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions 48-RuleOfFive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@
class A{
public:
A(); //default constructor.
//A() = default; //use compiler generated constructor.
//A() = delete; //no 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;
int *a;
};

A::A(): a{0}{
A::A(): A{0}{
std::cout << "default constructor." << std::endl;
}

A::A(int x): a{x}{
A::A(int x): a{new int{x}}{
std::cout << "user defined constructor." << std::endl;
}

A::~A(){
A::~A(){
std::cout << "destructor." << std::endl;
delete a;
}

A::A(const A& x): a{x.a}{
Expand All @@ -33,26 +36,41 @@ A::A(const A& x): a{x.a}{

A::A(A&& x): a{x.a}{
std::cout << "move constructor." << std::endl;
delete x.a;
}

A& A::operator=(const A& x){
A t{x.a};
A t{*x.a};
std::cout << "copy assignment." << std::endl;
return t;
}

A& A::operator=(A&& x){
A t{x.a};
A t{*x.a};
std::cout << "move assignment." << std::endl;
return t;
}

int main(){
A a;
A b{1};
A c{a};
A a;
std::cout << std::endl;

A b{1};
std::cout << std::endl;

A c{a};
std::cout << std::endl;

A d{};
std::cout << std::endl;

a = b;
d = std::move(a);
std::cout << std::endl;

d = std::move(a); //stl function that returns rvalue reference for given argument.
std::cout << std::endl;

A e{std::move(a)};
std::cout << std::endl;

}

0 comments on commit 83619ca

Please sign in to comment.