Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: C++
Expertise: Intermediate
Jun 11, 1998



Application Security Testing: An Integral Part of DevOps

That's what friends are for

A class can declare external functions or other classes as friends. Friendship grants full access to all of the grantor's members, even private and protected ones:
void encrypt (string & rep) {/*..*/} //global function

class spy { 
			static void transmit(const string& rep) { /*..*/}

class secret {
		friend class spy;//spy can access all members of 'secret'
		friend void encrypt(string & rep);//...and so can encrypt
		string report; 
		void scramble() { ::encrypt(report); }
		void transmit() const { spy::transmit(report); }
Notes about friendship:
  1. A friend declaration exposes implementations details of its class, so it should be used wisely. However, friendship has the advantage of allowing code re-use in a simple manner; in fact, many of the standard functions and overloaded operators are used in standard containers (like string<>) by means of friendship.
  2. Friendship is not inherited, so non-public members of any class derived from secret are not accessible to spy and encrypt.
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date