회원 로그인
|
참조 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개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 405 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 436 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 656 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 433 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 581 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 807 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 475 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 885 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 750 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 536 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 452 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 485 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 693 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1246 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 653 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 673 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 566 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 740 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 627 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 509 | 2020.06.02 23:21 |