회원 로그인
|
참조 ref, out (with Unity)
참조 ref, out
- C++의 레퍼런스 문법과 같은 기능을 수행함
- 단 ref로 연결할 변수는 반드시 초기화 되어 있어야 함
out
- C++의 레퍼런스 문법과 같은 기능을 수행함
- ref 문법과 같으나 초기화를 하지 않아도 됨
[기억클래스]
- 자동 변수 : 함수의 선언된 매개변수로 함수 호출시 생성하고 호출이 종료될때 자동 소멸함
- 지역변수 : 블럭안에서 선언된 변수로 사용범위가 블록 내부로 한정됨
- 전역 변수 : 생략 (설명만)
- 정적 변수 : 클래스 설명 부분에서 설명함
- 클래스 멤버 변수 : 클래스 설명 부분에서 설명함
using UnityEngine; using System.Collections; public class Out_test : MonoBehaviour { void Start() { int a = 10; // 생성(초기화) 시키고 참조함 -> ref int b; // 생성을 요청해서 참조함 -> out Function1(ref a); Function2(out b); } // 기본 메소드 void Function1(ref int value) { // 초기화된 변수 출력 Debug.Log("value : " + value); value = 10; // ref : 할당된 메모리를 찹조함 } // out 메소드 void Function2(out int value) { // out : 할당을 먼저해서 참조를 확보한 후 value = 10; // 출력 Debug.Log("value : " + value); } }
using UnityEngine; using System.Collections; public class refout : MonoBehaviour { void Start () { int value1 = 10; int value2 = 20; Debug.Log("[교체전] value1 : " + value1); Debug.Log("[교체전] value2 : " + value2); Swap(value1, value2); Debug.Log("[교체후] value1 : " + value1); Debug.Log("[교체후] value2 : " + value2); Debug.Log("[ref를 이용한 참조1]========================================="); Debug.Log("[교체전] value1 : " + value1); Debug.Log("[교체전] value2 : " + value2); // value1, value2를 참조로 넘겨줌 RefSwap(ref value1, ref value2); Debug.Log("[교체후] value1 : " + value1); Debug.Log("[교체후] value2 : " + value2); Debug.Log("[ref를 이용한 참조2]========================================="); int inputValue1 = 0; // ref 변수는 사용전에 반드시 초기화 해야함 int inputValue2 = 0; // int inputValue2; // inputValue1, inputValue2를 참조로 넘겨줌 RefInput(ref inputValue1, ref inputValue2); Debug.Log("[입력후] inputValue1 : " + inputValue1); Debug.Log("[입력후] inputValue2 : " + inputValue2); Debug.Log("[out를 이용한 참조2]========================================="); int inputValue3; // out 변수는 사용전에 초기화 하지 않아도 됨 int inputValue4; // inputValue3, inputValue4를 참조로 넘겨줌 OutInput(out inputValue3, out inputValue4); Debug.Log("[입력후] inputValue3 : " + inputValue3); Debug.Log("[입력후] inputValue4 : " + inputValue4); } // 일반 교체 함수 void Swap(int value1, int value2) { int temp = value1; value1 = value2; value2 = temp; } // ref를 이용한 교체 함수 void RefSwap(ref int value1, ref int value2) { int temp = value1; value1 = value2; value2 = temp; } // ref를 이용한 입력 함수 void RefInput(ref int inputValue1, ref int inputValue2) { inputValue1 = 30; inputValue2 = 50; } // out를 이용한 교체 함수 void OutInput(out int inputValue3, out int inputValue4) { inputValue3 = 30; inputValue4 = 50; } }
댓글 0개
| 엮인글 0개
285개(5/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
205 | [Python] Anaconda 사용 | 푸딩뱃살 | 3410 | 2017.08.28 17:51 |
204 | [Python] url 인코딩/디코딩 | 푸딩뱃살 | 6312 | 2017.08.12 19:23 |
203 | [Python] Qt5(ui) to py | 푸딩뱃살 | 2620 | 2017.07.04 15:29 |
202 | [VisualStudio] Visual Studio Community 2015 설치 파일 (다운로드) [1+1] | 푸딩뱃살 | 4496 | 2017.06.11 20:11 |
201 | [VisualStudio] Visual Studio Community 2015 삭제 | 푸딩뱃살 | 4270 | 2017.06.11 20:06 |
200 | [Python] PyQt4 to PyQt5 | 푸딩뱃살 | 2968 | 2017.06.02 16:50 |
199 | [Python] NumPy 라이브러리 설치 | 푸딩뱃살 | 3341 | 2017.05.18 15:14 |
198 | [Python] Python Decompiler | 푸딩뱃살 | 3933 | 2017.04.28 14:28 |
197 | [Python] 암호화 ASE | 푸딩뱃살 | 3425 | 2017.01.09 00:10 |
196 | [Python] 모듈 with xbox 360 controller | 푸딩뱃살 | 2096 | 2016.09.18 22:03 |
195 | [PHP] php 메모리 부족 (PHP Fatal error) | 푸딩뱃살 | 4299 | 2016.09.10 21:07 |
194 | [C#] 조건연산자 ?: | 푸딩뱃살 | 2937 | 2016.09.06 13:46 |
193 | [C#] object 형 변환 (with Unity) [1+1] | 푸딩뱃살 | 10529 | 2016.05.04 02:20 |
192 | [C#] 현재 시간 | 푸딩뱃살 | 3028 | 2016.02.28 23:23 |
191 | [C#] 문자열 Format | 푸딩뱃살 | 2461 | 2016.01.21 12:10 |
190 | [XML] XML 파싱 (with Unity) | 푸딩뱃살 | 7031 | 2016.01.20 10:33 |
189 | [PHP] PHP 전송 종류 | 푸딩뱃살 | 2402 | 2016.01.11 10:49 |
188 | [C#] 배열 | 푸딩뱃살 | 1199 | 2016.01.05 14:38 |
>> | [C#] 참조 ref, out (with Unity) | 푸딩뱃살 | 6068 | 2015.12.08 11:22 |
186 | [PHP] CodeIgniter(코드이그나이트) 사용 | 푸딩뱃살 | 11873 | 2015.12.06 17:46 |