From -5700458035375147579
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,2b0387157d35b1a9
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1995-01-20 21:20:37 PST
Path: nntp.gmd.de!newsserver.jvnc.net!howland.reston.ans.net!spool.mu.edu!olivea!koriel!male.EBay.Sun.COM!engnews2.Eng.Sun.COM!clamage
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Need advice on developing classes
Date: 21 Jan 1995 05:20:37 GMT
Organization: Sun Microsystems Inc., Mountain View, CA
Lines: 58
Message-ID: <3fq5j5$eor@engnews2.Eng.Sun.COM>
References: <D2ByGp.E4A@seas.ucla.edu> <1995Jan13.155558.4483@rcmcon.com> <790640862snz@thone.demon.co.uk>
NNTP-Posting-Host: taumet.eng.sun.com
X-Newsreader: NN version 6.5.0 #21 (NOV)

andys@thone.demon.co.uk (Andy Sawyer) writes:

>In article <1995Jan13.155558.4483@rcmcon.com>
>           rmartin@rcmcon.com "Robert Martin" writes:

>[snip]
>> 
>> Friendship cannot be stolen, only given. Its use is
>> to expose privates to a priviledged few, so that those privates do not
>> have to exposed to the world.
>> 
>how about:

>"x.h"
>class X {
>   static int si_XVal;
>   friend void TheFriend();
>};

[ code omitted showing different static functions called "TheFriend"
  in different translation units ]


The friend decl says there is one function. The program violates
the One-Definition Rule by providing different definitions. This
is an error which the compiler is not required to diagnose. The
program behavior is undefined.


>An even more worrying case (depending more on your linker than anything else)
>is that if TheFriend lies in a external library, the following is possible:

>"example.cpp"
>#include "x.h"

>void TheFriend()
>{
>  // Do what ya want in here!
>}

You can "hijack" any function this way, friend or not. It doesn't
violate any language rule. The declaration says there is one
function with a specified name and type. You provide one function
definition with that name and type. That satisfies the language
rules. If some other function with the same name and type exists
somewhere else in the world, that is not a C++ language issue,
as long as it doesn't get included in this program. (Then it
becomes a violation of the One-Definition Rule.)

The language rules do not specify how to build a program from
different translation units, or where to find external functions.

If as a library provider you wish to prevent hijacking, there
are various things you can do, from using hidden validation code
to putting the entire library in one object module. This is
all outside the C++ language definition.
--
Steve Clamage, stephen.clamage@eng.sun.com


