티스토리 뷰
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
- Object type casting
- ... in Javascript
- Javascript Algorithm
- C++
- hackerrank javascript solution
- spring boot application
- HashMap
- Collection Framework
- easy javascript algorithm
- 알고리즘
- hackerrank
- 프로그래머스
- rest parameter
- algorithm
- code refactoring
- java
- hackerrank javascript
- javascript
- equals()
- 프로그래머스 알고리즘문제
- repeat()
- substring()
- HackerRank Algorithm
- math.max
- hackerrank solution
- math.abs
- compareTo()
- string class in java
- 프로그래머스 알고리즘
- easy algorithm
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함