SlideShare a Scribd company logo
C++ Programming - 7th Study
C++ Programming - 7th Study
3
4
// 1
Person(const Person& src)
: height(src.height), weight(src.weight) { }
// 2
Person(const Person& src)
{
height = src.height;
weight = src.height;
}
int main()
{
Person p1(183.4, 78.5);
Person p2(p1);
}
5
C++ Programming - 7th Study
7
8
9
10
11
Person& operator=(const Person& rhs)
{
if (this == &rhs)
return *this;
height = rhs.height;
weight = rhs.weight;
return *this;
}
int main()
{
Person p1(183.4, 78.5), p2(175.6, 68.3);
p1 = p2;
}
12
C++ Programming - 7th Study
14
15
Person p1 Person p2
int roomNum
3
183.4
double height
78.5
double weight
int* room int roomNum
5
175.6
double height
68.3
double weight
int* room
스택(Stack) 메모리 힙(Heap) 메모리
16
Person p1 Person p2
int roomNum
3
183.4
double height
78.5
double weight
int* room int roomNum
5
175.6
double height
68.3
double weight
int* room
스택(Stack) 메모리 힙(Heap) 메모리
주인이 없어진 메모리!
(Orphaned Memory)
17
Person p1 Person p
int roomNum
3
183.4
double height
78.5
double weight
int* room int roomNum
3
183.4
double height
78.5
double weight
int* room
스택(Stack) 메모리 힙(Heap) 메모리
printRoom는 방의 정보를 출력하는 함수
함수가 호출되면서 얕은 복사로 인해
포인터만 복사되고 데이터는 복사되지 않음
18
Person p1
int roomNum
3
183.4
double height
78.5
double weight
int* room
스택(Stack) 메모리 힙(Heap) 메모리
반환 해제된 메모리
→ 댕글링 포인터(Dangling Pointer)
printRoom 함수가 리턴하면
스택 객체인 p의 소멸자가 호출되면서
room 포인터가 가리키는 메모리를 해제함
19
20
class Person {
private:
int* room;
int roomNum;
double height;
double weight;
public:
Person() { }
Person(int _roomNum, double _height, double _weight)
: roomNum(_roomNum), height(_height), weight(_weight) {
room = new int[roomNum];
}
~Person() {
delete[] room;
}
};
21
int main()
{
Person p1(3, 183.4, 78.5);
Person p2(5, 175.6, 68.3);
p1 = p2;
}
22
void printRoom(Person p) { }
int main()
{
Person p1(3, 183.4, 78.5);
printRoom(p1);
}
23
Person(const Person& src)
: roomNum(src.roomNum), height(src.height), weight(src.weight)
{
room = new int[roomNum];
for (int i = 0; i < roomNum; ++i)
room[i] = src.room[i];
}
24
Person& operator=(const Person& rhs) {
if (this == &rhs)
return *this;
delete[] room;
room = nullptr;
height = rhs.height;
weight = rhs.weight;
roomNum = rhs.roomNum;
room = new int[roomNum];
for (int i = 0; i < roomNum; ++i)
room[i] = rhs.room[i];
return *this;
}
25
C++ Programming - 7th Study
https://github.com/utilForever/ModernCpp/blob/master/ModernCpp/Classes/ruleOfZero.cpp
https://github.com/utilForever/ModernCpp/blob/master/ModernCpp/Classes/ruleOfFive.cpp
27

More Related Content

PDF
C++ Programming - 6th Study
PDF
C++ Programming - 8th Study
PDF
Programming Language: Ruby
PDF
6 windows-phone8-introduction-m6-slides
PDF
FormValidator::LazyWay で検証ルールをまとめよう
PDF
Couchdb
TXT
Litebox
C++ Programming - 6th Study
C++ Programming - 8th Study
Programming Language: Ruby
6 windows-phone8-introduction-m6-slides
FormValidator::LazyWay で検証ルールをまとめよう
Couchdb
Litebox

What's hot (19)

PDF
How to calculate the optimal undo retention in Oracle
PPT
Mysql DBI
PDF
言語の設計判断
PDF
Зависимые типы в GHC 8. Максим Талдыкин
PPTX
Everyday's JS
PDF
Demo: Real-Time Recommendations With Neo4j, Kafka, and Snowplow
PDF
Coding with Vim
PDF
刘平川:【用户行为分析】Marmot实践
PDF
20090622 Vimm4
PDF
MongoDB全機能解説2
PDF
MongoUK - PHP Development
PPTX
Developing 2D Games with Stage3D
DOCX
Program membalik kata
ZIP
Web+GISという視点から見たGISの方向性
PDF
Angular Refactoring in Real World
TXT
[Php] navigations
DOC
Object oriented mysqli connection function
How to calculate the optimal undo retention in Oracle
Mysql DBI
言語の設計判断
Зависимые типы в GHC 8. Максим Талдыкин
Everyday's JS
Demo: Real-Time Recommendations With Neo4j, Kafka, and Snowplow
Coding with Vim
刘平川:【用户行为分析】Marmot实践
20090622 Vimm4
MongoDB全機能解説2
MongoUK - PHP Development
Developing 2D Games with Stage3D
Program membalik kata
Web+GISという視点から見たGISの方向性
Angular Refactoring in Real World
[Php] navigations
Object oriented mysqli connection function
Ad

Viewers also liked (13)

PDF
C++ Programming - 9th Study
PDF
C++ Programming - 10th Study
PDF
C++ Programming - 13th Study
PDF
C++ Programming - 12th Study
PDF
C++ Programming - 11th Study
PDF
Data Structure - 2nd Study
PDF
C++ Programming - 14th Study
PDF
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
PDF
Data Structure - 1st Study
PDF
[C++ Korea 2nd Seminar] C++17 Key Features Summary
PDF
[제1회 시나브로 그룹 오프라인 밋업] 개발자의 자존감
PDF
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
PDF
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
C++ Programming - 9th Study
C++ Programming - 10th Study
C++ Programming - 13th Study
C++ Programming - 12th Study
C++ Programming - 11th Study
Data Structure - 2nd Study
C++ Programming - 14th Study
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
Data Structure - 1st Study
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[제1회 시나브로 그룹 오프라인 밋업] 개발자의 자존감
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
Ad

Similar to C++ Programming - 7th Study (14)

PDF
C programming.   For this code I only need to add a function so th.pdf
PDF
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
PPT
Unit 6
DOCX
DS Code (CWH).docx
ODT
Logic Equations Resolver J Script
PDF
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
PDF
Doubly Linked List
PDF
public class Person { private String name; private int age;.pdf
PPTX
Using Arbor/ RGraph JS libaries for Data Visualisation
DOCX
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
PDF
I have the following code and I need to know why I am receiving the .pdf
PPTX
Intro to Ember.JS 2016
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
C programming.   For this code I only need to add a function so th.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
Unit 6
DS Code (CWH).docx
Logic Equations Resolver J Script
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Doubly Linked List
public class Person { private String name; private int age;.pdf
Using Arbor/ RGraph JS libaries for Data Visualisation
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
I have the following code and I need to know why I am receiving the .pdf
Intro to Ember.JS 2016
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf

More from Chris Ohk (20)

PDF
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
PDF
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
PDF
Momenti Seminar - 5 Years of RosettaStone
PDF
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
PDF
Momenti Seminar - A Tour of Rust, Part 2
PDF
Momenti Seminar - A Tour of Rust, Part 1
PDF
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
PDF
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
PDF
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
PDF
Proximal Policy Optimization Algorithms, Schulman et al, 2017
PDF
Trust Region Policy Optimization, Schulman et al, 2015
PDF
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
PDF
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
PDF
[RLKorea] <하스스톤> 강화학습 환경 개발기
PDF
[NDC 2019] 하스스톤 강화학습 환경 개발기
PDF
C++20 Key Features Summary
PDF
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
PDF
디미고 특강 - 개발을 시작하려는 여러분에게
PDF
청강대 특강 - 프로젝트 제대로 해보기
PDF
[NDC 2018] 유체역학 엔진 개발기
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
Momenti Seminar - 5 Years of RosettaStone
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 1
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Proximal Policy Optimization Algorithms, Schulman et al, 2017
Trust Region Policy Optimization, Schulman et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기
C++20 Key Features Summary
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
디미고 특강 - 개발을 시작하려는 여러분에게
청강대 특강 - 프로젝트 제대로 해보기
[NDC 2018] 유체역학 엔진 개발기

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Machine learning based COVID-19 study performance prediction
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Programs and apps: productivity, graphics, security and other tools
Spectral efficient network and resource selection model in 5G networks
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Chapter 3 Spatial Domain Image Processing.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

C++ Programming - 7th Study

  • 3. 3
  • 4. 4
  • 5. // 1 Person(const Person& src) : height(src.height), weight(src.weight) { } // 2 Person(const Person& src) { height = src.height; weight = src.height; } int main() { Person p1(183.4, 78.5); Person p2(p1); } 5
  • 7. 7
  • 8. 8
  • 9. 9
  • 10. 10
  • 11. 11
  • 12. Person& operator=(const Person& rhs) { if (this == &rhs) return *this; height = rhs.height; weight = rhs.weight; return *this; } int main() { Person p1(183.4, 78.5), p2(175.6, 68.3); p1 = p2; } 12
  • 14. 14
  • 15. 15
  • 16. Person p1 Person p2 int roomNum 3 183.4 double height 78.5 double weight int* room int roomNum 5 175.6 double height 68.3 double weight int* room 스택(Stack) 메모리 힙(Heap) 메모리 16
  • 17. Person p1 Person p2 int roomNum 3 183.4 double height 78.5 double weight int* room int roomNum 5 175.6 double height 68.3 double weight int* room 스택(Stack) 메모리 힙(Heap) 메모리 주인이 없어진 메모리! (Orphaned Memory) 17
  • 18. Person p1 Person p int roomNum 3 183.4 double height 78.5 double weight int* room int roomNum 3 183.4 double height 78.5 double weight int* room 스택(Stack) 메모리 힙(Heap) 메모리 printRoom는 방의 정보를 출력하는 함수 함수가 호출되면서 얕은 복사로 인해 포인터만 복사되고 데이터는 복사되지 않음 18
  • 19. Person p1 int roomNum 3 183.4 double height 78.5 double weight int* room 스택(Stack) 메모리 힙(Heap) 메모리 반환 해제된 메모리 → 댕글링 포인터(Dangling Pointer) printRoom 함수가 리턴하면 스택 객체인 p의 소멸자가 호출되면서 room 포인터가 가리키는 메모리를 해제함 19
  • 20. 20
  • 21. class Person { private: int* room; int roomNum; double height; double weight; public: Person() { } Person(int _roomNum, double _height, double _weight) : roomNum(_roomNum), height(_height), weight(_weight) { room = new int[roomNum]; } ~Person() { delete[] room; } }; 21
  • 22. int main() { Person p1(3, 183.4, 78.5); Person p2(5, 175.6, 68.3); p1 = p2; } 22
  • 23. void printRoom(Person p) { } int main() { Person p1(3, 183.4, 78.5); printRoom(p1); } 23
  • 24. Person(const Person& src) : roomNum(src.roomNum), height(src.height), weight(src.weight) { room = new int[roomNum]; for (int i = 0; i < roomNum; ++i) room[i] = src.room[i]; } 24
  • 25. Person& operator=(const Person& rhs) { if (this == &rhs) return *this; delete[] room; room = nullptr; height = rhs.height; weight = rhs.weight; roomNum = rhs.roomNum; room = new int[roomNum]; for (int i = 0; i < roomNum; ++i) room[i] = rhs.room[i]; return *this; } 25