C ++ and friendship

    I discovered a funny fact: it turns out that classes in C ++ can have imaginary friends. Example: Note that there is no Joe class declaration in the program, but the program compiles without errors (and without warnings). That is, Joe does not exist, he can be called Fred's imaginary friend. This is possible because a friendship relationship only affects the compilation of a class that is declared by a friend. And a class that declares someone as a friend will be compiled the same way, regardless of what their friends are and whether they exist.

    class Fred {

        friend class Joe;

    public:

        void dance() {

            //какой-то код

        }

    };



    int main() {

        Fred guy;

        guy.dance();

        return 0;

    }

    //конец программы




    Also popular now: