friend keyword์ ๋ํด์ ๊ฐ๋จํ๊ฒ ๋ด์ฉ์ ์ ๋ฆฌํ๊ณ ๋์ด๊ฐ๋ ค ํฉ๋๋ค.
ํด๋์ค ๋ฉค๋ฒ ์ค์๋ private๋ก ์ ์ธ๋ ๋ฉค๋ฒ๋ค์ด ์์ ๊ฒ๋๋ค.
ํ์ง๋ง ํด๋์ค ์ธ๋ถ์์๋ ๊ถํ์ด ์์ผ๋ฏ๋ก ํด๋น ๋ฉค๋ฒ์ ์ ๊ทผํ์ง ๋ชปํฉ๋๋ค..
์ด ๋ ํด๋์ค ์ธ๋ถ์์ ํด๋น private member๋ค์ ์ ๊ทผํ ์ ์๋๋ก ํด์ฃผ๋ ๊ฒ์ด ๋ฐ๋ก friend ํค์๋์ ๋๋ค.
ํด๋น ํค์๋๋ ์๋์ ๋ฐฉ๋ฒ์ฒ๋ผ ์ฌ์ฉ์ด ๊ฐ๋ฅํฉ๋๋ค.
๊ฐ๋จํ ์์๋ ํจ๊ป ๋ณด๊ฒ ์ต๋๋ค.
#include <iostream>
class Information {
public:
int updateValue(int newValue) {
int oldValue=value;
value=newValue;
return oldValue;
}
private:
friend void friendAccess();
void tempFunction(){}; // private๋ก ๊ฐ๋ ค์ง tempfunction
int value;
};
void friendAccess() {
Information info;
info.tempFunction();
info.value=14;
}
int main() {
return 0;
}
ํด๋น ์์๋ฅผ ๋ณด๋ฉด ํจ์์ ์ธ๋ถ์์ private๋ก ์ ์ธ๋ tempFunction๊ณผ value๋ฅผ ์ ๊ทผํ๋ ๋ชจ์ต์ ๋ณผ ์ ์์ต๋๋ค.
๋น์ฐํ ์ค๋ฅ๋ ์ ๋ฉ๋๋ค.
์ ๋ ์ฌ๊ธฐ์ ๊ถ๊ธ์ฆ์ด ์๊ฒผ๋๋ฐ, friendํค์๋๋ฅผ private์์์ ์์ฑํด์ผํ๋ ๊ท์น์ด ์์๊น? ์์ต๋๋ค.
ํ์ง๋ง public์์์ ์์ฑํด๋ ๊ฒฐ๊ณผ๋ ๋๊ฐ๋๊ตฐ์. (์ค๋ฅ X)
'๐พ CS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[html, JS] input์ ์ ํจ์ฑ ๊ฒ์ฌ (0) | 2023.11.09 |
---|---|
[์์นํ๋ก๊ทธ๋๋ฐ] ํฉ๊ธ๋ถํ ํ์๋ฒ (0) | 2023.10.28 |
์ต์ ํ ์ ์, ์์(๊ฐ๋จ) (0) | 2023.10.28 |
[C++] noexcept Keyword (0) | 2023.10.17 |
[C++] explicit Keyword (0) | 2023.10.07 |