Pass by Reference

๋‚ด๊ฐ€ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ํด๋ž˜์Šค๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด๋•Œ, ์ด ํด๋ž˜์Šค์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”, ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋ผ ํ–ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋ฅผ ํด๋ž˜์Šค ๋ฐ–์˜ ํ•จ์ˆ˜์—์„œ input์œผ๋กœ ๋ฐ›๋Š”๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

๋ณดํ†ต ํด๋ž˜์Šค๋ฅผ ํ•จ์ˆ˜์˜ input parameter๋กœ ๋ฐ›์„ ๋•Œ, reference๋กœ ๋ฐ›๋Š”๋‹ค. ๊ทธ ์ด์œ ๋Š” ํด๋ž˜์Šค๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ณต๊ฐ„์˜ ์ ˆ์•ฝ์„ ์œ„ํ•ด์„œ์ด๋‹ค.

const

์™ธ๋ถ€ ํ•จ์ˆ˜์—์„œ ๋‚ด๊ฐ€ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๋ถ€๋ฅผ ๋•Œ, callbyreference๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์™”๋‹ค๋ฉด, ํ•จ์ˆ˜ ๋‚ด์—์„œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ, input ํŒŒ๋ผ๋ฏธํ„ฐ ์•ž์—ย constย ๋ฅผ ๋‹ฌ์•„์ค€๋‹ค.

void print(const Point& pt){
    // ํ•จ์ˆ˜๋‚ด์šฉ
}

๊ทธ๋Ÿฐ๋ฐ, ์ด ํ•จ์ˆ˜๋‚ด์—์„œ ํ•จ์ˆ˜์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์ด ํ•„์š”ํ•œ๋ฐ, const๋กœ ์ด ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด const๋ผ๋Š” ํ‘œ์‹œ๊ฐ€ ์žˆ๋Š” method๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// class ์ •์˜
class Point {
private:
    int x;
    int y;
    
public:
    Point(): x(0), y(0) {}
    Point(int _x, int _y): x(_x), y(_y) {}
   
    // ์™ธ๋ถ€ํ•จ์ˆ˜์— ๊ฐ์ฒด๋ฅผ const๋กœ ๋„˜๊ฒผ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜
    int getX() const {return this->x;}
    int getY() const {return this->y;} 
    
// ์™ธ๋ถ€ ํ•จ์ˆ˜ ์ •์˜
void print(const Point& pt){
    cout << pt.getX() << ", " << pt.getY() << endl << endl;
}
 

Pointer to Object

ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ฌ ๋•Œ๋„ ์—ญ์‹œ ํฌ์ธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋˜‘๊ฐ™๋‹ค.

*

์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ „์— ํฌ์ธํ„ฐ์— ๋Œ€ํ•ด์„œ ๋ฐฐ์šธ๋•Œ, ํ•ด๋‹น ์ฃผ์†Œ์˜ ์žˆ๋Š” ๊ฐ’์œผ๋กœ ์ ํ”„ ํ•˜๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, *ย ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์—ˆ๋‹ค.

ํด๋ž˜์Šค ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์ด ๊ฒƒ์„ ์ทจํ•˜๊ฒŒ ๋˜๋ฉด ํด๋ž˜์Šค ๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋งํ•˜๊ฒŒ ๋˜๊ณ , ๊ทธ์ œ์„œ์•ผ ์šฐ๋ฆฌ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

#include <iostream>
 
using namespace std;
 
class Point{
public:
    int x;
    int y;
};
 
int main(){
    Point pt1;
    Point* ppt1;
    
    pt1.x = 1;
    pt1.y = 2;
    
    ppt1 = &pt1;
    
    cout << (*ppt1).x << ", " << (*ppt1).y << endl;
    
    return 0;
    
}
1, 2

โ†’

์ด๋ฒˆ์—๋Š” ํฌ์ธํ„ฐ์—์„œ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ ‘๊ทผํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›Œ๋ณด์ž.

#include <iostream>
 
using namespace std;
 
class Point{
public:
    int x;
    int y;
};
 
int main(){
    Point pt1;
    Point* ppt1;
    
    pt1.x = 1;
    pt1.y = 2;
    
    ppt1 = &pt1;
    
	cout << ppt1->x << ", " << ppt1->y << endl;
    
    return 0;
    
}
1, 2

ํฌ์ธํ„ฐ ๋ณ€์ˆ˜(์ฃผ์†Œ๊ฐ€ ๋‹ด๊ธด) โ†’ ๋ณ€์ˆ˜ ์ด๋Ÿฐ์‹์œผ๋กœ ์ ์–ด์ฃผ๊ฒŒ ๋˜๋ฉด, โ€œํ•ด๋‹น ํฌ์ธํ„ฐ ์ฃผ์†Œ๋กœ ๊ฐ€์„œ, ๋ณ€์ˆ˜ x์˜ ๊ฐ’์„ ์ฝ์–ด!โ€ ๋ผ๋Š” ๋œป์ด ๋œ๋‹ค.

๋™์ ํ• ๋‹น new, delete

๋™์ ํ• ๋‹น๋„ ์—ญ์‹œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋™์ ํ• ๋‹น์€, ํ•œ ํ•จ์ˆ˜๋‚ด์—์„œ ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ „์—๋„ ๊ฐ’์„ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ๋„ฃ์—ˆ๋‹ค๊ฐ€ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์–ด ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.

#include <iostream>
 
using namespace std;
 
class Point{
public:
    int x;
    int y;
};
 
int main(){
    Point* pt1, pt2;
    
    pt1 = new Point;
    pt2 = new Point[5];
    
    pt1.x = 1;
    pt1.y = 2;
    
    cout << ppt1->x << ", " << ppt1->y << endl;
    
    delete pt1;
    delete pt2;
    
    return 0;
}

์ฃผ์˜์‚ฌํ•ญ

๋™์ ํ• ๋‹น์„ ํ•ด์ œํ•˜์ง€ ์•Š์œผ๋ฉด, ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋œ ์ดํ›„์—๋„ ๋‚จ์•„์žˆ์œผ๋ฏ€๋กœ! stream ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ผญ! delete๋กœ ํ• ๋‹น์„ ํ•ด์ œย ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค!

this Pointer

์ž๊ธฐ ์ž์‹ ์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ class๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด, ํŠน์ • ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ์ž๊ธฐ์ž์‹ ์„ ๋‚ด๋ฑ‰์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๊ฒƒ์„ ์œ„ํ•ด์„œ ์šฐ๋ฆฌ๋Š” ๋งค์„œ๋“œ ์•ˆ์—์„œย thisย ๋ผ๋Š” ํฌ์ธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์‚ฌ์šฉํ•œ๋‹ค.

#include <iostream>
 
using namespace std;
 
class Point{
public:
    int x;
    int y;
    
    // ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ž์‹ ์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
    void setXY(int _x, int _y){
        this->x = _x;
        this->y = _y;
    }
    
    // const ๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์—์„œ ์ผ์„ ๋•Œ const๋กœ ๋ฐ›๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ํ•จ์ˆ˜
    // 
    int getX() const {return this->x;}
    int getY() const {return this->y;}
};