Re: Problem with friend function and gcc 4.2 with objective-c++
Re: Problem with friend function and gcc 4.2 with objective-c++
- Subject: Re: Problem with friend function and gcc 4.2 with objective-c++
- From: "Clark Cox" <email@hidden>
- Date: Sat, 9 Aug 2008 07:32:13 -0700
On Sat, Aug 9, 2008 at 2:20 AM, Thomas Engelmeier
<email@hidden> wrote:
>
> Am 08.08.2008 um 00:09 schrieb Ken Worley:
>
>> Hi all,
>>
>> I'm using Xcode 3.1 and just switched to gcc 4.2 from 4.0, but I've run
>> into a problem with friend functions when compiling in objective-c++. I
>> contrived an example that illustrates the problem:
>>
> [...]
>>
>> This project builds fine using gcc 4.0, but when I switch the compiler
>> setting to use gcc 4.2, I get the errors listed below. Any clues would
>> certainly be appreciated if I'm doing something wrong. If not, I guess I'll
>> file a bug...
>>
>> Here's main.m:
>>
>> #import <Cocoa/Cocoa.h>
>>
>> class test1
>> {
>> public:
>> friend test1* newtest1(int x)
>> {
>> test1* anobj = new test1();
>> anobj->finishinit(x);
>> return anobj;
>> }
>> [...]
>> };
>>
>> int main(int argc, char *argv[])
>> {
>> test1* tobj = newtest1(5);
>> delete tobj;
>>
>> return NSApplicationMain(argc, (const char **) argv);
>> }
>
> friend != static, and even then this probably would not be valid semantics.
>
> test1* tobj = newtest1(5); has nothing to do with
> test1::newtest1( int ) or aTest1Instance->newtest1( int )
I believe that you are incorrect. This is a perfectly valid way of
defining friend functions in C++. Defined this way, newtest1 is a
global function (i.e. it is not scoped to test) that is allowed to
access the private/protected parts of test1 instances.
From the C++ standard (11.4 paragraph 5):
"
A function can be defined in a friend declaration of a class if and
only if the class is a non-local class (9.8),
the function name is unqualified, and the function has namespace
scope. [Example:
class M {
friend void f() { } //definition of globalf, a friend ofM,
//not the definition of a member function
};
—end example] Such a function is implicitlyinline. Afriendfunction
defined in a class is in the
(lexical) scope of the class in which it is defined.
"
If I were the original poster, I would file a bug.
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden