티스토리 뷰
Friend
Friend allows access to members and methods declared private or protected by other classes or functions of other classes. It is used only in unavoidable situations such as operator overloading because it is in violation of encapsulation.
Example code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using namespace std; class Apple; //forward declaration class Banana{ private: int m_count = 3; public: void print(Apple& a); //declaration }; class Apple { private: int m_count = 6; friend void Banana::print(Apple& a); //only Banana function is a friend. }; void Banana::print(Apple& a) { // definition cout << a.m_count << endl; }; int main() { Apple a; Banana b; b.print(a); //6 return 0; } | cs |
'C++' 카테고리의 다른 글
Vector & List in C++ (0) | 2018.11.22 |
---|---|
Virtual Function in C++ (0) | 2018.11.22 |
Templates (Function Template & Class Template) in C++ (0) | 2018.11.10 |
Exception handling (Try Throw and Catch) in C++ (0) | 2018.11.09 |
Inline function in C++ (0) | 2018.11.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- math.abs
- hackerrank
- rest parameter
- java
- string class in java
- javascript
- Collection Framework
- substring()
- easy algorithm
- C++
- hackerrank solution
- hackerrank javascript
- Object type casting
- HackerRank Algorithm
- equals()
- HashMap
- 프로그래머스 알고리즘문제
- easy javascript algorithm
- Javascript Algorithm
- code refactoring
- 프로그래머스
- algorithm
- repeat()
- math.max
- 프로그래머스 알고리즘
- spring boot application
- compareTo()
- hackerrank javascript solution
- ... in Javascript
- 알고리즘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함