admin
  • 0
Begginer

What is the friend keyword in C++?

  • 0

What is the friend keyword in C++?

  1. The friend keyword in C++ allows the programmer to declare friend functions and classes.

    class frnd{
    	private:
    		void somefunc();
    };
    
    int frndFUNC(int a) {}
    
    class A{
    	friend class frnd;
    	friend int frndFUNC(int a);
    };
    
    
    

    Some important points about friend functions and classes:

    1. Declarations of friend functions and classes are always made inside the class definition(can be anywhere either in public and private)
    2. Friend functions and classes are declared by the implementor i.e. only the class can define its friend functions and classes, a function or class cannot declare itself a friend of a class
    3. Friend classes can access the data members of the class but not vice versa i.e. Class frnd can access the data members of class A but class A cannot access members of class frnd.
    4. Friend functions are not inherited.
    • 0
Leave an answer

Leave an answer

Browse