📜 ⬆️ ⬇️

C ++ and friendship

I discovered a funny fact: it turns out that classes in C ++ can have imaginary friends. Example:

class Fred { <br>
friend class Joe; <br>
public : <br>
void dance() { <br>
//- <br>
} <br>
}; <br>
<br>
int main () { <br>
Fred guy; <br>
guy.dance(); <br>
return 0; <br>
} <br>
// <br>


Note that there is no Joe class declaration in the program, however the program is compiled without errors (and without warnings). That is, Joe does not exist, it can be called Fred's imaginary friend. This is possible because the friendship only affects the compilation of a class that is declared friend. A class that has declared someone to be their friend will be compiled equally regardless of what their friends are and whether they exist.

')

Source: https://habr.com/ru/post/63559/


All Articles