Microsoft PowerPoint - 9ÀÏ°_ÂüÁ¶ÀÚ.ppt

Similar documents
Microsoft PowerPoint - 8ÀÏ°_Æ÷ÀÎÅÍ.ppt

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

Microsoft PowerPoint - additional01.ppt [호환 모드]

11장 포인터

C++ Programming

설계란 무엇인가?

쉽게 풀어쓴 C 프로그래밍

untitled

Microsoft PowerPoint - C++ 5 .pptx

1. auto_ptr 다음프로그램의문제점은무엇인가? void func(void) int *p = new int; cout << " 양수입력 : "; cin >> *p; if (*p <= 0) cout << " 양수를입력해야합니다 " << endl; return; 동적할

[ 마이크로프로세서 1] 2 주차 3 차시. 포인터와구조체 2 주차 3 차시포인터와구조체 학습목표 1. C 언어에서가장어려운포인터와구조체를설명할수있다. 2. Call By Value 와 Call By Reference 를구분할수있다. 학습내용 1 : 함수 (Functi

C++ Programming

슬라이드 1

Microsoft PowerPoint - [2009] 02.pptx

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - ch07 - 포인터 pm0415

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

금오공대 컴퓨터공학전공 강의자료

Microsoft PowerPoint - Chapter 6.ppt

Microsoft PowerPoint - 제11장 포인터(강의)

(Microsoft PowerPoint - 07\300\345.ppt [\310\243\310\257 \270\360\265\345])

설계란 무엇인가?

Microsoft PowerPoint - chap06-2pointer.ppt

Microsoft PowerPoint - 제11장 포인터

C프로-3장c03逞풚

PowerPoint Template

슬라이드 1

C++ Programming

Microsoft PowerPoint - additional08.ppt [호환 모드]

K&R2 Reference Manual 번역본

Microsoft PowerPoint - additional07.ppt [호환 모드]

C++ Programming

<322EBCF8C8AF28BFACBDC0B9AEC1A6292E687770>

설계란 무엇인가?

chap10.PDF

PowerPoint Presentation

Microsoft PowerPoint - additional06.ppt [호환 모드]

PowerPoint Template

Microsoft PowerPoint - chap10-함수의활용.pptx

Microsoft PowerPoint - ch07 - 포인터 pm0415

A Dynamic Grid Services Deployment Mechanism for On-Demand Resource Provisioning

목차 포인터의개요 배열과포인터 포인터의구조 실무응용예제 C 2

임베디드시스템설계강의자료 6 system call 2/2 (2014 년도 1 학기 ) 김영진 아주대학교전자공학과

The C++ Programming Language 4 장타입과선언 4.11 연습문제 Hello,world! 프로그램을실행시킨다. 프로그램이컴파일되지않으면 B3.1 을참고하자. #include<iostream> //#include 문, 헤더파일, 전처리지시

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - chap02-C프로그램시작하기.pptx

PowerPoint Template

제 11 장포인터 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

Microsoft PowerPoint - chap-11.pptx

Microsoft PowerPoint - Chapter 1-rev

Data Structure

080629_CFP °ø°³¿ë.hwp

Microsoft Word - FunctionCall

(Microsoft PowerPoint - 11\300\345.ppt [\310\243\310\257 \270\360\265\345])

PowerPoint 프레젠테이션

Microsoft PowerPoint - additional03.ppt [호환 모드]

윤성우의 열혈 TCP/IP 소켓 프로그래밍

Microsoft PowerPoint - chap11-포인터의활용.pptx

KNK_C_05_Pointers_Arrays_structures_summary_v02

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CC0E7B0EDB0FCB8AE5C53746F636B5F4D616E D656E74732E637070>

PowerPoint Presentation

The C++ Programming Language 5 장포인터, 배열, 구조체 5.9 연습문제 다음의선언문을순서대로작성해보자. 문자에대한포인터, 10개정수의배열, 10개정수의배열의참조자, 문자열의배열에대한포인터, 문자에대한포인터에대한포인터, 상수정수, 상수

1. 클래스와배열 int 형배열선언및초기화 int ary[5] = 1, 2, 3, 4, 5 ; for (int i = 0; i < 5; i++) cout << "ary[" << i << "] = " << ary[i] << endl; 5 장클래스의활용 1

3. 1 포인터란 3. 2 포인터변수의선언과사용 3. 3 다차원포인터변수의선언과사용 3. 4 주소의가감산 3. 5 함수포인터

1. 표준입출력 C++ : C의모든라이브러리를포함 printf, scanf 함수사용가능예 : int, double, 문자열값을입력받고출력하기 #include <cstdio> int ivar; double dvar; char str[20]; printf("int, dou

11장 포인터

class Sale void makelineitem(productspecification* spec, int qty) SalesLineItem* sl = new SalesLineItem(spec, qty); ; 2. 아래의액티비티다이어그램을보고 Java 또는 C ++,

Microsoft PowerPoint - ch09 - 연결형리스트, Stack, Queue와 응용 pm0100

chap 5: Trees

4 장클래스와객체 클래스와객체 public과 private 구조체와클래스객체의생성과생성자객체의소멸과소멸자생성자와소멸자의호출순서디폴트생성자와디폴트소멸자멤버초기화멤버함수의외부정의멤버함수의인라인함수선언 C++ 프로그래밍입문

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

PowerPoint 프레젠테이션

Microsoft PowerPoint - chap03-변수와데이터형.pptx

C 언어 프로그래밊 과제 풀이

A Hierarchical Approach to Interactive Motion Editing for Human-like Figures

JAVA PROGRAMMING 실습 08.다형성

Microsoft PowerPoint - Chapter 10.ppt

설계란 무엇인가?

C++ Programming

2002년 2학기 자료구조

1 장 C 언어복습 표준입출력배열포인터배열과포인터함수 const와포인터구조체컴파일러사용방법 C++ 프로그래밍입문

(Microsoft Word - \301\337\260\243\260\355\273\347.docx)

프입2-강의노트-C++기초

제 14 장포인터활용 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

Chapter 4. LISTS

PowerPoint Presentation

Lab 3. 실습문제 (Single linked list)_해답.hwp

02장.배열과 클래스

Microsoft PowerPoint - CSharp-10-예외처리

유니티 변수-함수.key

BACK TO THE BASIC C++ 버그 헌팅: 버그를 예방하는 11가지 코딩 습관

Microsoft PowerPoint - Chap12-OOP.ppt

구조체정의 자료형 (data types) 기본자료형 (primitive data types) : char, int, float 등과같이 C 언어에서제공하는자료형. 사용자정의자료형 (user-defined data types) : 다양한자료형을묶어서목적에따라새로운자료형을

No Slide Title

adfasdfasfdasfasfadf

Frama-C/JESSIS 사용법 소개

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Template

Transcription:

참조자 - 참조자란무엇인가? - 참조자는어떻게만들고사용하는가? - 참조자는포인터와무엇이다른가? 1

참조자 (reference) 란? 참조자 : 객체 ( 타겟 ) 에대한다른이름 ( 별칭 ) 참조자의변화는타겟의변화 참조자만들고반드시초기화 참조자생성 형참조연산자 (&) 참조자이름 = 초기화타겟명 모든항목필수 예 ) int &rsomeref = someint ; 참조자에대한주소연산자 (&) 사용 참조사주소 = 타겟의주소 참조자에대입하기 ( 새로운타겟설정 ) 불능 2

#include <iostream> list 9.1 int main() using namespace std; int intone; int &rsomeref = intone; intone = 5; cout << "intone: " << intone << endl; cout << "rsomeref: " << rsomeref << endl; rsomeref = 7; cout << "intone: " << intone << endl; cout << "rsomeref: " << rsomeref << endl; return 0; 3

#include <iostream> list 9.2 int main() using namespace std; int intone; int &rsomeref = intone; intone = 5; cout << "intone: " << intone << endl; cout << "rsomeref: " << rsomeref << endl; cout << "&intone: " << &intone << endl; cout << "&rsomeref: " << &rsomeref << endl; return 0; 4

#include <iostream> list 9.3 int main() using namespace std; int intone; int &rsomeref = intone; intone = 5; cout << "intone:\t" << intone << endl; cout << "rsomeref:\t" << rsomeref << endl; cout << "&intone:\t" << &intone << endl; cout << "&rsomeref:\t" << &rsomeref << endl; int inttwo = 8; rsomeref = inttwo; cout << "\nintone:\t" << intone << endl; cout << "inttwo:\t" << inttwo << endl; cout << "rsomeref:\t" << rsomeref << endl; cout << "&intone:\t" << &intone << endl; cout << "&inttwo:\t" << &inttwo << endl; cout << "&rsomeref:\t" << &rsomeref << endl; return 0; 5

무엇이참조될수있는가? 참조대상 ( 사용자정의한것을포함한 ) 모든객체 int &rintref = int ; (X) int howbig = 200 ; int &rintref = howbig ; (O) Cat &rcatref = Cat ; (X) Cat Frisky ; Cat &rcatref = Frisky (O) 포인터와달리참조자는널 (null) 이될수없음 6

#include <iostream> list 9.4 class SimpleCat public: SimpleCat (int age, int weight); ~SimpleCat() int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage; int itsweight; ; SimpleCat::SimpleCat(int age, int weight) itsage = age; itsweight = weight; 7

int main() SimpleCat Frisky(5,8); SimpleCat & rcat = Frisky; std::cout << "Frisky is: "; std::cout << Frisky.GetAge() << " years old. \n"; std::cout << "And Frisky weights: "; std::cout << rcat.getweight() << " pounds. \n"; return 0; 8

함수인자 (argument) 를참조자로전달 함수의한계 매개변수에값으로전달 (call by value) 반환값이하나밖에없음 참조에의한전달 (call by reference) 포인터를이용하는방법 참조자를전달하는방법 포인터를사용할때의기능 + 일반변수사용시의편리성동시제공 9

#include <iostream> list 9.5 using namespace std; void swap(int x, int y); int main() int x = 5, y = 10; cout <<"Main. Before swap, x:"<<x<< "y:"<<y<<"\n"; swap(x,y); cout <<"Main. After swap, x: <<x<<" y: "<<y<<"\n"; return 0; void swap (int x, int y) int temp; cout <<"Swap. Before swap, x:"<<x<<" y: "<<y<<"\n"; temp = x; x = y; y = temp; cout <<"Swap. After swap, x: "<<x<<" y: "<<y<< "\n"; 10

#include <iostream> list 9.6 using namespace std; void swap(int *x, int *y); int main() int x = 5, y = 10; cout <<"Main. Before swap, x: "<<x<<" y: "<<y<<"\n"; swap(&x,&y); cout << "Main. After swap, x: "<<x<<" y: "<<y<<"\n"; return 0; void swap (int *px, int *py) int temp; cout << "Swap. Before swap, *px: " << *px << " *py: " << *py << "\n"; temp = *px; *px = *py; *py = temp; cout << "Swap. After swap, *px: " << *px << " *py: " << *py << "\n"; 11

#include <iostream> list 9.7 using namespace std; void swap(int &x, int &y); int main() int x = 5, y = 10; cout <<"Main. Before swap, x: "<<x<<" y: "<<y<<"\n"; swap(x,y); cout <<"Main. After swap, x: "<<x<<" y: "<<y<<"\n"; return 0; void swap (int &rx, int &ry) int temp; cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << "\n"; temp = rx; rx = ry; ry = temp; cout << "Swap. After swap, rx: " << rx << " ry: " << ry << "\n"; 12

함수헤더와함수원형 (prototype) 에대한이해 함수원형을통해참조에의한전달임을알수있음 C++ 의목적은프로그래머들이잘만들어진클래스와함수들에대해그내부구조는모르는채잘쓸수있게하는것이다. 이각부분들은전체프로그램을만들기위해조립만되면되는것이다. 마치건물과다리를만들때재료들을가져다조립만하면되듯이말이다. 똑같은방식으로기술자는파이프의하중, 용량, 크기등을결정할때사양서만보면된다. 똑같은방식으로프로그래머는클래스나함수의인터페이스부분만보고, 어떤서비스를하는지어떤매개변수를갖는지반환값이무엇인지를알게된다. 13

여러개의값을반환하는방법 함수는하나의값을반환 해결책 값을반환받기위한참조자 / 포인터를함수에전달 return 을통한반환은오류코드등의다른목적위해사용 14

#include <iostream> list 9.8 using namespace std; short Factor(int n, int* psquared, int* pcubed); int main() int number, squared, cubed; short error; cout << "Enter a number (0-20): "; cin >> number; error = Factor(number, &squared, &cubed); if (!error) cout << "number: " << number << "\n"; cout << "square: " << squared << "\n"; cout << "cubed: " << cubed << "\n"; else cout << "Error encountered!!\n"; return 0; 15

short Factor(int n, int *psquared, int *pcubed) short Value = 0; if (n > 20) Value = 1; else *psquared = n*n; *pcubed = n*n*n; Value = 0; return Value; 16

#include <iostream> list 9.9 using namespace std; typedef unsigned short USHORT; enum ERR_CODE SUCCESS, ERROR ; ERR_CODE Factor(USHORT, USHORT&, USHORT&); int main() USHORT number, squared, cubed; ERR_CODE result; cout << "Enter a number (0-20): "; cin >> number; result = Factor(number, squared, cubed); if (result == SUCCESS) cout << "number: " << number << "\n"; cout << "square: " << squared << "\n"; cout << "cubed: " << cubed << "\n"; else cout << "Error encountered!!\n"; return 0; 17

ERR_CODE Factor(USHORT n, USHORT &rsquared, USHORT &rcubed) if (n > 20) return ERROR; else rsquared = n*n; rcubed = n*n*n; return SUCCESS; 18

효율성을위해참조자를전달 함수에객체를전달할때마다그객체의복사가발생 ( 복사생성자함수호출 ) 함수로부터객체가반환될때도복사발생 시간 / 메모리소요 ( 큰객체일수록비용증대 ) 참조 (call by reference) 방식을쓰는이유 1. 값의변경을위해 2. 효율성증대를위해 19

#include <iostream> list 9.10 using namespace std; class SimpleCat public: SimpleCat (); SimpleCat(SimpleCat&); ~SimpleCat(); ; SimpleCat::SimpleCat() cout << "Simple Cat Constructor...\n"; SimpleCat::SimpleCat(SimpleCat&) cout << "Simple Cat Copy Constructor...\n"; SimpleCat::~SimpleCat() cout << "Simple Cat Destructor...\n"; 20

SimpleCat FunctionOne (SimpleCat thecat); SimpleCat* FunctionTwo (SimpleCat *thecat); int main() cout << "Making a cat...\n"; SimpleCat Frisky; cout << "Calling FunctionOne...\n"; FunctionOne(Frisky); cout << "Calling FunctionTwo...\n"; FunctionTwo(&Frisky); return 0; SimpleCat FunctionOne(SimpleCat thecat) cout << "Function One. Returning...\n"; return thecat; SimpleCat* FunctionTwo (SimpleCat *thecat) cout << "Function Two. Returning...\n"; return thecat; 21

상수포인터 (const pointer) 전달 객체의포인터전달시효율성증대 그러나, 원치않은값의변경가능 ( 위험요소 ) 상수포인터를사용함으로써값의변경차단 상수참조자사용시보다쉽게구현 22

#include <iostream> list 9.11 using namespace std; class SimpleCat public: SimpleCat(); SimpleCat(SimpleCat&) cout << "Simple Cat Copy Constructor...\n"; ~SimpleCat(); cout << "Simple Cat Destructor...\n"; int GetAge() const return itsage; void SetAge(int age) itsage = age; private: int itsage; ; SimpleCat::SimpleCat() cout << "Simple Cat Constructor...\n"; itsage = 1; 23

const SimpleCat * const FunctionTwo (const SimpleCat * const thecat); int main() cout << "Making a cat...\n"; SimpleCat Frisky; cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; int age = 5; Frisky.SetAge(age); cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; cout << "Calling FunctionTwo...\n"; FunctionTwo(&Frisky); cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; return 0; const SimpleCat * const FunctionTwo (const SimpleCat * const thecat) cout << "Function Two. Returning...\n"; cout << "Frisky is now " << thecat->getage(); cout << " years old \n"; // thecat->setage(8); const! return thecat; 24

#include <iostream> list 9.12 using namespace std; class SimpleCat public: SimpleCat(); SimpleCat(SimpleCat&) cout << "Simple Cat Copy Constructor...\n"; ~SimpleCat(); cout << "Simple Cat Destructor...\n"; int GetAge() const return itsage; void SetAge(int age) itsage = age; private: int itsage; ; SimpleCat::SimpleCat() cout << "Simple Cat Constructor...\n"; itsage = 1; 25

const SimpleCat &FunctionTwo (const SimpleCat & thecat); int main() cout << "Making a cat...\n"; SimpleCat Frisky; cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; int age = 5; Frisky.SetAge(age); cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; cout << "Calling FunctionTwo...\n"; FunctionTwo(&Frisky); cout <<"Frisky is "<<Frisky.GetAge()<<" years old\n"; return 0; const SimpleCat &FunctionTwo (const SimpleCat & thecat) cout << "Function Two. Returning...\n"; cout << "Frisky is now " << thecat.getage(); cout << " years old \n"; // thecat.setage(8); const! return thecat; 26

참조자와포인터를사용하는때 사용하기쉬움 참조자 - 선호 ( 가능하면매개변수및반환값을참조자로사용 ) 포인터 참조자에 비해 사용하기 번거로움 상대적으로융통성부족 - 재할당되지않음 -Null 이될수없음 융통성을제공 - 재할당가능 -Null 이가능 New에의한자유기억공간할당시사용 참조자 (&) / 포인터 (*) 선언시기호의위치 Cat& Frisky ; Cat & Frisky ; Cat &Frisky ; 27

범위를벗어난객체에대한참조자반환오류 참조자사용시 참조자는무엇을가리키는가? 그객체가지금존재하는가? 28

#include <iostream> list 9.13 class SimpleCat public: SimpleCat (int age, int weight); ~SimpleCat() int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage; int itsweight; ; SimpleCat::SimpleCat(int age, int weight) itsage = age; itsweight = weight; 29

SimpleCat &TheFunction(); int main() SimpleCat &rcat = TheFunction(); int age = rcat.getage(); std::cout << "rcat is " << age << " years old!\n"; return 0; SimpleCat &TheFunction() SimpleCat Frisky(5,9); return Frisky; 30

힙에있는객체에대한참조자반환 힙에있는객체에대한참조자반환시 메모리 ( 힙 ) 제거에신경 메모리제거후참조자사용중지 힙사용시포인터사용바람직 31

#include <iostream> list 9.14 class SimpleCat public: SimpleCat (int age, int weight); ~SimpleCat() int GetAge() return itsage; int GetWeight() return itsweight; private: int itsage; int itsweight; ; SimpleCat::SimpleCat(int age, int weight) itsage = age; itsweight = weight; 32

int main() SimpleCat & rcat = TheFunction(); int age = rcat.getage(); std::cout << "rcat is " << age << " years old!\n"; std::cout << "&rcat: " << &rcat << std::endl; // How do you get rid of that memory? SimpleCat * pcat = &rcat; delete pcat; // Uh oh, rcat now refers to?? return 0; SimpleCat &TheFunction() SimpleCat * pfrisky = new SimpleCat(5,9); std::cout << "pfrisky: " << pfrisky << std::endl; return *pfrisky; 33