Everything2
Near Matches
Ignore Exact
Full Text
Everything2

C++: the scope of friendship

created by ariels

(thing) by ariels (5.2 d) (print)   ?   (I like it!) 1 C! Mon Sep 30 2002 at 14:16:25

C++ lets you grant functions the same privileges of member functions by declaring them "friends" of a class (or struct). What are friends for? They let the implementor of the class write non-member functions without exposing the implementation to all callers. friends are a Good Thing: they enhance modularity.

Obviously, the conscientious programmer will want to keep as few friends as let her code her class' API. In some (good!) designs, some friends exist only as part of the implementation of some larger feature. The natural approach is to declare such functions, which should only be seen inside one filetranslation unit, as static.

You can't declare a static friend.

The design will thus be flawed.

As usual, there are excellent reasons for this: It's not just a misfeature or a wart; the entire language is so hideously contorted that allowing static friends would be even more dangerous than not allowing them.

Recall that a class declaration is usually situated in a header file (it will always be there in situations where modularity is a problem). The header file is #included everywhere in the code. And friends are declared in the header file. So every single file using the class knows about its friends.

Suppose you had a static friend. I could define, in some other file than that where you define your friend, another function with the same name and signature. Your header file would declare my function its friend, too! Merely by knowing that a function is a static friend, I could achieve unlimited access to its internals! By attempting to adjust the language to allow us a bit more modularity, we'd destroy all opportunities for hiding. And you cannot put your class' friends in some anonymous namespace: exactly the same problem would apply, as there is no special way of identifying the particular function in an anonymously-namespace with which your class really wants to be friends.

Allowing only extern friends solves this: You can only link one extern function with a given name and signature. So, having declared and defined your friends, any compiled and linked program will be guaranteed free of this subversion.

Of course, if your OS lets you use dynamic libraries (DLLs) in some way, you can probably overcome this limitation at run-time. But such perversions are outside the scope of ISO C++...


printable version
chaos

Friends who fuck The C++ Programming Language Freakshow The Computer Is Your Friend Software is free speech
friend It's better to be lonely than to be with inferior people Outlook Express E2 guide to music you've never heard of
Keep your friends close and your enemies closer With a Little Help from My Friends DLL Nerds, geeks, dorks and losers
the optimizer is your friend wart static Mohammed Saeed al-Sahhaf
dynamic library Catalan translation unit LISP
How to receive email in Outlook Express Misfeature Joey API
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
What you are reading:
Information War is coming: whose side are you on?
How to peel and devein shrimp
Wives, submit to your husbands
penny-farthing
reincarnation
The Incredible String Band
Wow! signal
Marathon 2: Durandal
Savannah, the aftermath
Hamlet Chicken Plant Disaster
Dutch Revolt
Multiplication tables
Did Ronald Reagan end the Cold War?
New Writeups
TheOrientalAfrican
The Soft Meadow of my Childhood(event)
BookReader
The Dragon Slayers(fiction)
kohlcass
religiously fashionable(review)
Pavlovna
waulking song(thing)
tentative
Stick Man(poetry)
Ereneta
The Fight with the Snapping Turtle: Or, the American St. George(poetry)
sitaraika
Fog and fire(personal)
MonoliTheory
She sobs in response(fiction)
kohlcass
Arzu(person)
Clarke
Duster(fiction)
mountain boy
Oscar Diaz v. Delvin Rodriguez(person)
jessicaj
A time to shoot(fiction)
ushdfgakjasgh
You Are Not Being Fair(recipe)
aneurin
Mentioned in despatches(idea)
Venkman
One in Five(fiction)
Everything 2 is brought to you by the letter C and The Everything Development Company