CppModule 01

์ฐธ์กฐ(Reference)

1. ์ฐธ์กฐ์˜ ํ‘œ๊ธฐ

A Reference Is an Alias

์ฐธ์กฐ๋Š” ๊ฐ์ฒด(๋ฐ ๋ณ€์ˆ˜)๋“ค์˜ ๋ณ„์นญ์ด๋ฉฐ ์•„๋ž˜์ฒ˜๋Ÿผ ์ •์˜์™€ ๋™์‹œ์— ์ดˆ๊ธฐํ™”๋˜์–ด์•ผํ•œ๋‹ค.

int ival = 1024;
int &refVal = ival;	// refVal refers to (is another name for) ival
int &refVal2;		// !!!error: a reference must be initialized
// ok: refVal3 is bound to the object to which refVal is bound, i.e., to ival
int &refVal3 = refVal;
// initializes i from the value in the object to which refVal is bound
int i = refVal;		// ok: initializes i to the same value as ival
int &refVal4 = 10;	// !!!error: initializer must be an object
double dval = 3.14;
int &refVal5 = dval;	// !!!error: initializer must be an int object
  1. ์ฐธ์กฐ๋Š” ์ •์˜์™€ ๋™์‹œ์— ์ดˆ๊ธฐํ™” ๋œ๋‹ค.
  2. ์ฐธ์กฐ๋Š” r-value ๋ฐ NULL๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ• ์ˆ˜ ์—†๋‹ค(์ฐธ์กฐ ๋งค๊ฐœ์ธ์ž๋กœ๋„ r-value, NULL๋ถˆ๊ฐ€).
  3. ์ฐธ์กฐ๋Š” ์ดˆ๊ธฐํ™” ๋ ๋•Œ ๊ฐ’์ด ๋ณต์‚ฌ๋˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฐ๊ฒฐ(bind)๋œ๋‹ค.
  4. ํ•œ๋ฒˆ ์ดˆ๊ธฐํ™”๋˜๋ฉด ๋ณ€๊ฒฝํ• ์ˆ˜ ์—†๋‹ค.

2. const์™€ ์ฐธ์กฐ

void	func(obj &a, const obj &b, obj const &c){...}

3. ์ฐธ์กฐ์™€ ํฌ์ธํ„ฐ์˜ ์ฐจ์ด(์ฐธ์กฐ์˜ ํ•„์š”์„ฑ)

ํฌ์ธํ„ฐ๋Š” ํŠน์ • ์ฃผ์†Œ๋ฅผ ์ €์žฅํ• ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ

๋”ฐ๋ผ์„œ ์ฐธ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ ํฌ์ธํ„ฐ๋ณด๋‹ค ์œ ๋ฆฌํ•˜๋‹ค

์ฐธ๊ณ  : C++ Primer 5th, ์†Œ๋…„์ฝ”๋”ฉ

4. ๋„ค์ž„์ŠคํŽ˜์ด์Šค, std, stream

4.1 ๋„ค์ž„์ŠคํŽ˜์ด์Šค

4.2 stream

5. fiostream๊ณผ stringstream

fiostream์‚ฌ์šฉ๋ฒ•. ์‚ฌ์šฉํ•œ ํ•จ์ˆ˜

5.2 stringstream

#include ์— ์ •์˜๋˜์–ด์žˆ์Œ.

The sstream header defines three types to support in-memory IO; these types read from or write to a string as if the string were an IO stream.

์ด ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์„ I/O stream์ฒ˜๋Ÿผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค. fiostream์˜ I/O ๋ฒ„ํผ์— ์ ‘๊ทผํ•˜๋ฉฐ ์ž‘์—…ํ•˜๋Š”๊ฑด ๋ณด์กฐ๊ธฐ์–ต์žฅ์น˜์— ๋งค๋ฒˆ ์ ‘์†ํ•˜๋ฉฐ ์ž‘์—…ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋Š” ๋งŽ์€ ์ž์›๊ณผ ์‹œ๊ฐ„์˜ ๋‚ญ๋น„์ด๋ฏ€๋กœ, I/O๋ฒ„ํผ์˜ ๋‚ด์šฉ์„ ์ตœ๋Œ€ํ•œ stringstream์œผ๋กœ ์˜ฎ๊ฒจ์™€์„œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ž‘์—…ํ•˜๋Š”๊ฒŒ ์—ฌ๋Ÿฌ๋ชจ๋กœ ์œ ๋ฆฌํ•˜๋‹ค.

์ด์™ธ์—๋„ ๋ณ€ํ™˜, ์น˜ํ™˜โ€ฆ์œ ๋ฆฌํ•œ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ถ”๊ฐ€ ์ •๋ฆฌ stringstream ์‚ฌ์šฉ๋ฒ• ์˜ˆ์‹œ ๋งํฌ :

https://bigpel66.oopy.io/library/42/inner-circle/12 https://modoocode.com/215