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

What's hot (19)

PDF
How to calculate the optimal undo retention in Oracle
Jorge Batista
 
PPT
Mysql DBI
Joe Christensen
 
PDF
言語の設計判断
nishio
 
PDF
Зависимые типы в GHC 8. Максим Талдыкин
Юрий Сыровецкий
 
PPTX
Everyday's JS
Adib Mehedi
 
PDF
Demo: Real-Time Recommendations With Neo4j, Kafka, and Snowplow
Neo4j
 
PDF
Coding with Vim
Enzo Wang
 
PDF
刘平川:【用户行为分析】Marmot实践
taobao.com
 
PDF
20090622 Vimm4
id774
 
PDF
MongoDB全機能解説2
Takahiro Inoue
 
PDF
MongoUK - PHP Development
Boxed Ice
 
PPTX
Developing 2D Games with Stage3D
Mike Jones
 
DOCX
Program membalik kata
haqiemisme
 
ZIP
Web+GISという視点から見たGISの方向性
Hidenori Fujimura
 
PDF
Angular Refactoring in Real World
bitbank, Inc. Tokyo, Japan
 
TXT
[Php] navigations
Vishal Gurujuwada
 
DOC
Object oriented mysqli connection function
clickon2010
 
How to calculate the optimal undo retention in Oracle
Jorge Batista
 
Mysql DBI
Joe Christensen
 
言語の設計判断
nishio
 
Зависимые типы в GHC 8. Максим Талдыкин
Юрий Сыровецкий
 
Everyday's JS
Adib Mehedi
 
Demo: Real-Time Recommendations With Neo4j, Kafka, and Snowplow
Neo4j
 
Coding with Vim
Enzo Wang
 
刘平川:【用户行为分析】Marmot实践
taobao.com
 
20090622 Vimm4
id774
 
MongoDB全機能解説2
Takahiro Inoue
 
MongoUK - PHP Development
Boxed Ice
 
Developing 2D Games with Stage3D
Mike Jones
 
Program membalik kata
haqiemisme
 
Web+GISという視点から見たGISの方向性
Hidenori Fujimura
 
Angular Refactoring in Real World
bitbank, Inc. Tokyo, Japan
 
[Php] navigations
Vishal Gurujuwada
 
Object oriented mysqli connection function
clickon2010
 

Viewers also liked (13)

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

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

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

More from Chris Ohk (20)

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

Recently uploaded (20)

DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 

C++ Programming - 7th Study